views:

321

answers:

3

hello everybody! 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.

A: 

No built-in way to do this. You'll have to extend the Upload library.

Update: By built-in I meant that the Codeigniter library doesn't support just passing it names of the additional thumbnails. Personally I prefer to extend a given functionality so it can be used somewhere else in the project. Don't repeat yourself style.

zbrox
how do i do that?
sonill
See "Extending Core Class" here http://codeigniter.com/user_guide/general/core_classes.html
zbrox
+2  A: 

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 );
Summer
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
Does your thumbnail_generator function move or delete the original file?
Summer
i dont know if its moving or deleting. when i checkrd there was only 1 image in 1 folder only.
sonill
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
+2  A: 

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