Help with creating thumbnails in a module
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
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?
$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; } } }
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?
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.How can I stop the data insert if there is an error while uploading the file?
$config['create_thumb'] = FALSE;
, it still create the thumb with the _thumb suffix, why is that?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.
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.
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.
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
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.
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.
$this->upload_path() is short for:
function upload_path() { return(assets_server_path('gallery/','images')); }
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.
I want an odd shaped image to be say: 100x100 but maintain aspect and crop to that ratio before resizing.