3

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.

1
  • Can you provide your thumbnail code?
    – Gazler
    May 3, 2010 at 8:36

3 Answers 3

12

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
    }
}
4

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 );
4
  • 1
    i 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???
    – sonill
    May 4, 2010 at 6:07
  • Does your thumbnail_generator function move or delete the original file?
    – Summer
    May 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.
    – sonill
    May 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. :)
    – Summer
    May 5, 2010 at 14:37
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.