Help with creating thumbnails in a module

edited September 2012 in News & Announcements
Hello,

I have a module where I give the user the option to upload an image and I want to automatically create a thumbnail from this image.

I've looked for help and found this post: http://www.getfuelcms.com/forums/discussion/878

I've changed my form_fields from this: $fields['image']['class'] = 'asset_select images/logos'; to this: $fields['image'] = array('type' => 'asset', 'folder' => 'images/logos', 'hide_options' => FALSE); But the image fields on the form is the same as before.

I downloaded the userguide from the link in the post, extracted it into fuel/modules/userguide folder but when I go to the user guide I get a page full of php errors so I can't see any forms section.

Can you please help me and show me how to automatically create a thumbnail from the uploaded image?

Thank you.

Comments

  • edited 10:51PM
    I've tried that, the image uploads but no thumbnail is being created:
    function on_after_post($values) { if (empty($values['image'])) return; // process any uploaded images files that have been specified foreach($_FILES as $file) { if (is_image_file($file['name'])) { $this->load->library('image_lib'); $config['source_image'] = assets_path() . 'images/logos/'.$file['name']; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $this->image_lib->initialize($config); if ( ! $this->image_lib->resize()) { $error = $this->image_lib->display_errors(); $this->validator->catch_error($error); echo $error; die; } else { echo 'Success'; die; } } }
    The word success is printed but there is not thumbnail.

    Any help?
  • edited 10:51PM
    Tried that as well, 'Success' is printed but still no thumbnail:
    $config = array( 'source_image' => APPPATH . 'images/logos/'.$file['name'], 'new_image' => APPPATH . 'images/logos/thumbs', 'maintain_ration' => true, 'width' => 150, 'height' => 100 ); $this->load->library('image_lib', $config); if ( ! $this->image_lib->resize()) { $error = $this->image_lib->display_errors(); $this->validator->catch_error($error); echo $error; die; } else { echo 'Success'; echo APPPATH . 'assets/images/logos'; die; } } }
  • edited 10:51PM
    Another question:

    My field in the table is called: image, so in the form I automatically get two fields:
    Upload image with a select image button and then
    ...Or upload an image with a Browse button.

    How do I get rid of the Upload image with the select button?
  • edited September 2012
    ok, this post solved my problems: http://www.getfuelcms.com/blog/id/5 Now the only question is how to get rid of the select image field and button, if possible.
  • edited 10:51PM
    Another problem I have is that if the image I'm trying to load is too big I get an error message, but the data is still created in the logos table.

    How can I stop the data insert if there is an error while uploading the file?
  • edited 10:51PM
    Another problem, although I say $config['create_thumb'] = FALSE;, it still create the thumb with the _thumb suffix, why is that?
  • edited 10:51PM
    To get rid of the Upload image button you can add the following to your form_fields method:
    unset($fields['image_upload']);
    To not insert the data upon image upload error, you would need to do some validation on the image using the $_FILES array and mimicking the same validation the Image Upload library performs. You could use on_before_validate hook method and the add_validation method within that hook to run a custom validation function like so:
    ..... on_before_validate($values) { $this->add_validation('image', array($this, $my_func), 'Please upload an image of size xxxx by xxxx'); } function my_func($val) { // custom code return $val; } .....
    Also, the User Guide on GitHub is for the 1.0 beta and is not compatible with 0.93.
  • edited 10:51PM
    Thank you, but I'm not sure how to use the my_func.

    Not sure what is the $val that is being sent to it, and what should it return.

    Is there somewhere I can read about it?

    What I tried:
    function on_before_validate($values) { $this->add_validation('image_upload', array($this, $checkFileType), 'Please upload a jpg file only.'); } function checkFileType($val) { // custom code if($_FILES['file_ext'] != '.jpg') $val = false; else $val = true; return $val; }

    The record is not being saved to the database but I don't get any error either.
  • edited 10:51PM
    Another problem I have. I wanted to get rid of the select image button not, the upload image button.

    My code:
    function form_fields($values = array()) { $fields = parent::form_fields($values); $fields['image_upload']['upload_path'] = assets_server_path('logos', 'images'); $fields['image_upload'] = array('label'=>'Upload an Image', 'type'=>'file'); unset($fields['image']); if (!empty($values['image_upload'])) { $fields['image_upload']['before_html'] = '<div class="img_display"><img src="'.img_path('logos/'.$values['image_upload']).'" style="float: right;"/></div>'; } return $fields; }
    Now nothing is being uploaded.
  • edited 10:51PM
    Ok, solved my problem like this:
    In my model:
    function form_fields($values = array()) { $fields = parent::form_fields($values); $fields['image'] = array('label'=>'Upload an Image', 'type'=>'file'); $fields['image']['upload_path'] = assets_server_path('logos', 'images'); unset($fields['image_upload']); if (!empty($values['image'])) { $thumb_img = img_path('logos/thumbs/').$this->thumb_name($values['image']); $fields['image']['before_html'] = '<div class="img_display"><img src="'. $thumb_img . '" style="float: right;"/></div>'; } return $fields; }

    In fuel/modules/fuel/controllers/module.php I moved lines:
    // process $_FILES if (!$this->_process_uploads($posted)) { $this->session->set_flashdata('error', get_error()); redirect(fuel_uri($this->module_uri.'/edit/'.$id)); }
    from where they were to after this code:
    $this->_run_hook('before_create', $posted);

    Got this idea from this post: http://www.getfuelcms.com/forums/discussion/954/still-insert-data-to-database-with-errors-when-upload-images/#Item_5

    Thank you 66beta
  • edited October 2012
    I'm having a similar issue while resizing, using the new 1.0 route I have:
    function on_after_post($where) { $file = $where['item']; // resize and crop thumb file $config = array(); $config['source_image'] = $this->upload_path().$file; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $config['width'] = 80; $config['height'] = 80; $this->load->library('image_lib', $config); if (!$this->image_lib->resize_and_crop()) { print_r($config).'<br />'; echo file_exists($config['source_image']).'<br />'; echo $this->image_lib->display_errors('<p>','</p>'); die(); } return($where); }
    After uploading the file, which does exist on the file-system. I get an error:
    The path to the image is not correct.

    I'm not sure what's wrong here with resizing... the same thing happens with $this->image_lib->resize()

    If I then hit the back button and change function on_after_post($where) to: function on_before_save($where)
    the thumbnail is created.

    There appears to be something odd happening here.

    Thanks.
  • edited 10:51PM
    What's the value of $this->upload().$file?
  • edited October 2012
    That's the full server path to the file. Even if I hard code it to say: /var/server/path/to/file.jpg I get the same thing.

    Keep in mind that directs to the file that exists at this point.. but for some reason resize() or resize_and_crop() in on_after_post() can't seem to find it, but if the file exists and you change the function to on_before_save() it will find it and do the operation no problem.
  • edited 10:51PM
    So is that the server path to the image in the assets/images folder?
  • edited 10:51PM
    yes, assets/images/gallery/ to be exact.

    $this->upload_path() is short for: function upload_path() { return(assets_server_path('gallery/','images')); }
  • edited 10:51PM
    If you exit the script right before executing the contents of the on_after_post method, can you see the image in the assets/images/gallery/ folder?

    Also, are you familiar with the new 'asset' field type with 1.0? The section on Forms talks about the additional options you can use to configure it.
  • edited 10:51PM
    Okay, well then... goes to show when you don't read ALL the new instructions what great things you miss! haha... That is fantastic... gotta promote that asset field type more 'cause it's fantastic!
  • edited 10:51PM
    Well... to be fair to you, there is a lot of new stuff.
  • edited 10:51PM
    Is there a resize_and_crop option for the assets though?

    I want an odd shaped image to be say: 100x100 but maintain aspect and crop to that ratio before resizing.
  • edited 10:51PM
    There was a way to do it by passing 'resize_and_crop' as a parameter to fuel->assets->upload(). However, it wasn't quite integrated into the assets page very well. I made a few changes this morning to help with that so you should be able to pass the parameter "resize_method" => "resize_and_crop" and posted them to the repo.
Sign In or Register to comment.