CodeIgniter Forums
Resizing the same image twice. SOLVED. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Resizing the same image twice. SOLVED. (/thread-33466.html)



Resizing the same image twice. SOLVED. - El Forum - 08-26-2010

[eluser]HovaCarlito[/eluser]
Heya,

I'm probably overlooking something or missing something. I'm making a simple CMS where an uploaded file gets resized twice - once to make sure the file isn't huge and another time to make the thumbnail.

The code:


Code:
function handleResize($path){
        $config['image_library'] = 'gd2';
        $config['source_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 375;
        $config['height']    = 500;
        $config['create_thumb'] = TRUE;
    
        $this->image_lib->initialize($config);
        
        if ( ! $this->image_lib->resize()){
            echo $this->image_lib->display_errors();
        }
        else{
            echo "done1";    
        }
        
        $this->image_lib->clear();
        
        $config_big['image_library'] = 'gd2';
        $config_big['source_image'] = $path;
        $config_big['maintain_ratio'] = TRUE;
        $config_big['width']     = 510;
        $config_big['height']    = 680;
        
        
    
        $this->image_lib->initialize($config_big);
        
        if ( ! $this->image_lib->resize()){
            echo $this->image_lib->display_errors();
        }
        else{
            echo "done2";
        }

    }

Now the result: any file I upload gets processed both times (it echo's both the resultmessages) but somehow it seems to use the width/height of the second config set (the $config_big) in the thumbnail and no resizing on the second pass. Am I missing something?

Thanks in advance!


Resizing the same image twice. SOLVED. - El Forum - 08-26-2010

[eluser]Twisted1919[/eluser]
I gave up using CI Image class for resizing long time ago, because it doesn't have that wonder of adaptive resize.
If you're intersted , my code looks like :
Code:
$thumb = PhpThumbFactory::create($file->full_path);
$thumb->adaptiveResize(800, 600);
if( ! $thumb->save($file->full_path) )
{
  //Manage error .
}
And run that piece of code as many times you need. Good bye distorted images.


Resizing the same image twice. SOLVED. - El Forum - 08-27-2010

[eluser]HovaCarlito[/eluser]
Thanks for the reply.

I was hoping to avoid not using CI's tools. Plus, of course, from a developer point of few I'd like to know what I do wrong Wink

EDIT:

OK, the problem seemed to be that I expected CI to clear all it's loaded configuration on clear(). Apparently it doesn't. I had to turn the thumb function off in the second pass.

Final, working code:

Code:
/*    handleResize handles the resizing of the photos.
        returns true on success, false on error */
        
    function handleResize($path){
        /* Configuration for the thumbnail */
        $config['image_library'] = 'gd2';
        $config['source_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 375;
        $config['height']    = 500;
        $config['create_thumb'] = TRUE;
    
        /* Initialize the configuration */
        $this->image_lib->initialize($config);
        
        /* Handle the resizing, if something went wrong, print the error and return false */
        if ( ! $this->image_lib->resize()){
            echo $this->image_lib->display_errors();
            return false;
        }
        
        /* Well, it does... something */
        $this->image_lib->clear();
        
        /* Settings for the big file */
        $config_big['image_library'] = 'gd2';
        $config_big['source_image'] = $path;
        $config_big['maintain_ratio'] = TRUE;
        $config_big['width']     = 510;
        $config_big['height']    = 680;
        $config_big['create_thumb']    = FALSE;

        /* Init the config */
        $this->image_lib->initialize($config_big);
        
        /* Resize it */
        if ( ! $this->image_lib->resize()){
            echo $this->image_lib->display_errors();
            return false;
        }
        
        /* Yay! It worked! Return true and dance */
        return true;

    }