CodeIgniter Forums
Problem resizing one original image two times - 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: Problem resizing one original image two times (/showthread.php?tid=8060)

Pages: 1 2


Problem resizing one original image two times - El Forum - 06-07-2008

[eluser]Roger Glenn[/eluser]
I was having similar problems resizing the original and creating a thumbnail.

I tried $this->image_lib->clear(); and $this->image_lib->initialize($config); but still had problems until I renamed the $config array.

Code:
// SNIP ----------
$this->load->library('image_lib');

print('about to create THUMBNAIL of ' . $this->photos_dir . $filename . '<br />');
$this->create_thumbnail($this->photos_dir . $filename);

print('about to resize ' . $this->photos_dir . $filename . '<br />');
$this->resize_image($this->photos_dir . $filename);
// SNIP -----------

// RESIZE IMAGE * * * * * * * * * * * * * * * * * * * * *
function resize_image($filepath) {
    // configure the ORIGINAL IMAGE
    $orig['image_library']  = 'gd2';
    $orig['source_image']   = $filepath;
    $orig['width']          = $orig['height'] = '600';
    $orig['master_dim']     = 'width';
    $orig['maintain_ratio'] = TRUE;
    $orig['create_thumb']   = FALSE;
    
    // load image manipulation class
    $this->image_lib->clear();
    $this->image_lib->initialize($orig);
    
    // resize original
    if ( ! $this->image_lib->resize()) {
        print($this->image_lib->display_errors().'<br />');
        return TRUE;
    } else {
        print('original image resized<br />');
        return FALSE;
    }
    
}

// CREATE THUMBNAIL * * * * * * * * * * * * * * * * * * * * *
function create_thumbnail($filepath) {
    // configure the THUMBNAIL IMAGE
    $config['image_library']  = 'gd2';
    $config['source_image']   = $filepath;
    $config['width']          = $config['height'] = '160';
    $config['master_dim']     = 'width';
    $config['maintain_ratio'] = TRUE;
    $config['create_thumb']   = TRUE;
    
    // load image manipulation class
    $this->image_lib->clear();
    $this->image_lib->initialize($config);
    
    // resize thumbnail image
    if ( ! $this->image_lib->resize()) {
        print($this->image_lib->display_errors().'<br />');
        return TRUE;
    } else {
        print('thumbnail created<br />');
        return FALSE;
    }
    
}

This finally worked for me.

I agree that the example in the docs should be a little more explicit about initialize();