can anyone tell me how to create 2 different thumbnails of same images with different sizes and location in codeigniter. I have created a upload function and another thumbnail generation function, it works fine but can't sort out how to create 2 different thumbnail at accordingly at once. any help will be appreciated.
3 Answers
It's actually pretty easy...
function create_thumbs()
{
$this->load->library('image_lib');
$path = "path/to/image/";
$source_image = "original.jpg";
$medium_image = "medium.jpg";
$small_image = "small.jpg";
// Resize to medium
$config['source_image'] = $path.$source_image;
$config['new_image'] = $path.$medium_image;
$config['width'] = 200;
$config['height'] = 200;
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
// an error occured
}
// Keep the same source image
$config['new_image'] = $path.$small_image;
$config['width'] = 50;
$config['height'] = 50;
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
// an error occured
}
}
So you have a thumbnail_generator function, and say it takes parameters original_file_name, new_file_name, and thumbnail_size.
Just call it twice!
thumbnail_generator( original_file.jpg, new_file_sm.jpg, 300 );
thumbnail_generator( original_file.jpg, new_file_xsm.jpg, 150 );
-
1i tried it, but it doesn't work, only 1 thumbnail is created. 1st function works and it created thumb but 2nd one doesn't. it does return true though but no image is created. I also tried calling 1st function in upload_model and 2nd in controller but doesnot work. if i disable 1st function then 2nd 1 works. any other suggestions? does codeigniter allow creation of multiple image resizing???– sonillMay 4, 2010 at 6:07
-
Does your thumbnail_generator function move or delete the original file?– SummerMay 4, 2010 at 15:00
-
i dont know if its moving or deleting. when i checkrd there was only 1 image in 1 folder only.– sonillMay 5, 2010 at 5:30
-
It sounds like maybe you copied some code from somewhere, and now you don't know how it works. Check out php's native function imagecopyresampled() and roll your own image resizer. It will probably be easier than coping with other people's code. :)– SummerMay 5, 2010 at 14:37
Just a little modification in the above answer. Add this in the beginning of your function
$this->load->library('image_lib');
$this->image_lib->clear();
the previous config needs to be cleared