CodeIgniter Forums
Image library: resizing two or more images - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Image library: resizing two or more images (/showthread.php?tid=25025)



Image library: resizing two or more images - El Forum - 11-27-2009

[eluser]tokyotech[/eluser]
I want to create two thumbnails from the same source image. The following code only makes my first thumbnail, but will not create the second one. Why is that? Is it because the config array is passed into the constructor and an object cannot be instantiated twice?

Code:
$config['image_library']  = 'gd2';
$config['source_image']   = $fullPath;
$config['create_thumb']   = true;
$config['maintain_ratio'] = true;
$config['width']          = 256;
$config['height']         = 256;
$config['master_dim']     = 'width';
$config['thumb_marker']   = IMAGE_LARGE;

$this->load->library('image_lib', $config);
$this->image_lib->resize();

$config['width']        = 128;
$config['height']       = 128;
$config['thumb_marker'] = IMAGE_SMALL;

$this->load->library('image_lib', $config);
$this->image_lib->resize();



Image library: resizing two or more images - El Forum - 11-28-2009

[eluser]markup2go[/eluser]
This may help:

Code:
$this->image_lib->clear()

"The clear function resets all of the values used when processing an image. You will want to call this if you are processing images in a loop." - User guide


Image library: resizing two or more images - El Forum - 11-28-2009

[eluser]nkm82[/eluser]
Also you should unset the first config array after the first resize (or name each one diferently).

Code:
$config['master_dim']     = 'width';
$config['thumb_marker']   = IMAGE_LARGE;

$this->load->library('image_lib', $config);
$this->image_lib->resize();
unset($config);

OR

Code:
$large_config = array();
$large_config['key'] = 'value';
// (...)
$this->load->library('image_lib', $large_config);

// (...)

$small_config = array();
$small_config['key'] = 'value';
// (...)
$this->load->library('image_lib', $small_config);



Image library: resizing two or more images - El Forum - 11-28-2009

[eluser]tokyotech[/eluser]
markup2go,

My new code just has one line extra (the clear call), but it still doesn't work. I also tried printing out errors, but none were reported.

Code:
$config['image_library']  = 'gd2';
$config['source_image']   = $fullPath;
$config['create_thumb']   = true;
$config['maintain_ratio'] = true;
$config['width']          = 256;
$config['height']         = 256;
$config['master_dim']     = 'width';
$config['thumb_marker']   = IMAGE_LARGE;

$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();

$config['width']        = 128;
$config['height']       = 128;
$config['thumb_marker'] = IMAGE_SMALL;

$this->load->library('image_lib', $config);
$this->image_lib->resize();

nkm82,

I would like to reuse the same array because most of the info remains the same. The only differences are filename and size.


Image library: resizing two or more images - El Forum - 11-28-2009

[eluser]Aken[/eluser]
Without specific errors it's hard to help much. But I can tell you that the second time you want to use the library, don't load it over again, just reinitialize it.

Instead of
Code:
$this->load->library('image_lib', $config);
Use
Code:
$this->image_lib->initialize($config);



Image library: resizing two or more images - El Forum - 11-28-2009

[eluser]demogar[/eluser]
I would use a function for it so I can reuse code and it would be just simplier to modify.

Code:
function something(){
    # Generate the thumbnail / resize the image
    $this->_createThumb('file', 250, 300, '/images/', MARKER);
}

function _createThumb($file, $width, $height, $path, $marker = NULL) {
    # Lib config
    $config['image_library']    = 'gd2';
    $config['source_image']        = $path . $file;
    $config['new_image']        = $path . 'thumbs/' . $file;
    $config['thumb_marker']        = $marker;
    $config['create_thumb']        = TRUE;
    $config['maintain_ratio']    = TRUE;
    $config['width']         = $width;
    $config['height']         = $height;
    
    # Load the library
    $this->load->library('image_lib', $config);
    
    # Resize the image and clear the lib
    $this->image_lib->resize();
    $this->image_lib->clear();
}

Cheers


Image library: resizing two or more images - El Forum - 11-28-2009

[eluser]tokyotech[/eluser]
Yes! It works now. Thanks everyone.


Image library: resizing two or more images - El Forum - 04-09-2010

[eluser]Marc Arbour[/eluser]
I am in a dead end with this...

I slightly adapted the above code.

Here is what I use in my controler:
Code:
in my calling function:

$this->_createThumb($file_name,100,100,$file_path,'',$file_path.'thumbs_small/', "width");
$this->_createThumb($file_name,800,300,$file_path,'',$file_path.'thumbs_large/', "height");

elsewhere in the controler

function _createThumb($file, $width, $height, $path, $marker = NULL, $newfilepath, $master_dim="auto") {
    # Lib config
    $config['master_dim'] = $master_dim;
    $config['image_library']    = 'gd2';
    $config['source_image']        = $path . $file;
    $config['new_image']        = $newfilepath . $file;
    $config['thumb_marker']        = $marker;
    $config['create_thumb']        = TRUE;
    $config['maintain_ratio']    = TRUE;
    $config['width']         = $width;
    $config['height']         = $height;
    
    # Load the library
    $this->load->library('image_lib', $config);
    
    # Resize the image and clear the lib
    $this->image_lib->resize();
    $this->image_lib->clear();
}

Only the 1st thumb is created. Wheter I change them around one another, the small or the large one gets created.

it seems the $this->image_lib->clear(); is not working properly.

Any ideas? (using latest CI version)

Duke


Image library: resizing two or more images - El Forum - 04-09-2010

[eluser]Marc Arbour[/eluser]
Hi.

I solved it with this code: http://ellislab.com/forums/viewthread/115907/