Welcome Guest, Not a member yet? Register   Sign In
Help with image lib - thumbnails
#1

[eluser]gRoberts[/eluser]
Hey all,

On a site I'm working on we are using the image lib to generate thumbnails.

The sites requirement is that we have 4 copies of the image:

images/thumb/image.ext = 100x100
images/med/image.ext = 250x250
images/large/image.ext = 800x600
images/image.ext = original image

I have created a class that uses the image lib, and it will resize the image to the width/height given, and save it to the path given.

This works fine if the image is larger then the given width/height but if the image is smaller then things start to go wrong.

At the moment I have told the class to simply copy the image rather then use the image lib to generate a thumbnail if the image is smaller then the required width/height but unfortunately some images are wider then the required width, or taller then the required height, which causes the script to copy an image that won't appear correctly because the width/height is wrong.

Has anyone had any experience with this?

Would you be able to see what i'm doing wrong, and possibly point out my flaws?

Thank you all in advance!

Gavin
#2

[eluser]gRoberts[/eluser]
Has anyone got any idea's?
#3

[eluser]megabyte[/eluser]
Create a function that resizes the image and then call it as many times as needed. Do not try to use the thumbnail option if you are trying to make more than one resize it wil not work in my experience.

Another issue you may come accross is memory usage problems. you may need to increase the memory each time the image lib is used.
#4

[eluser]Merolen[/eluser]
This is just my view after having a quick look at the code, I haven't tested this myself.

As far as I can see from the image_lib code, if you specify

Code:
$config['maintain_ratio'] = TRUE;
$config['width'] = '';
$config['height'] = '';
$this->load->library('image_lib',$config);

the original size is used and no resizing is done (well, it is, but with the original height and width)

So what you can do is to check if the image height and width of the picture is bigger than the size you want it resized to (check php.net for functions). Even if only one of the two axes are bigger you should still send it to resizing with new width and height.

Again, haven't tried this myself
#5

[eluser]gRoberts[/eluser]
Sorry for not getting back...

I've got a check in place that checks whether the image is smaller then the required width/height, and for some reason this was failing, or at least I thought it was.

We were getting images where the width was more then the required width but the height wasn't so it was being copied instead of resized.

I'm currently removing all the resized images (thumb, med and large) and regenerating them. If this works then i've solved my problem.

Thanks
#6

[eluser]megabyte[/eluser]
image lib auto checks if the image is bigger or not than the required settings and chooses to resize or not.
#7

[eluser]gRoberts[/eluser]
are you sure?

I'm getting 16x16px images being resized to 800x600...
#8

[eluser]megabyte[/eluser]
private message me, and I'll email you the code I use to upload and resize multiple images. I had huge issues figuring out why it was causing problems.
#9

[eluser]Merolen[/eluser]
Wouldn't it be nice to give the code so everyone could see? Just a suggestion Wink
#10

[eluser]megabyte[/eluser]
I have before in a previus post, I just can't remember where it is. I posted it after I had solved a problem I was asking for help with.

Here you go. I'm sure you will be able to work through it all and adopt it to your needs.

Code:
function _upload_image(){
        
        if(!empty($_FILES))
        {
        $this->load->library('upload');

        $config['upload_path'] = './photos/';
        $config['allowed_types'] = 'jpg|jpeg|gif|avi';
        //$config['encrypt_name'] = 'TRUE';
        $this->upload->initialize($config);                
        $this->upload->do_upload();
        return $this->upload->data();
        }        
    }

        
    function _resize_image($file){
        
        $this->load->library('image_lib');
        
        if($file != '')
        {
        $this->setMemoryForImage($file['full_path']);
        $config['image_library'] = 'GD2';
        $config['source_image'] = $file['full_path'];
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 800;
        $config['height'] = 800;
        $config['master_dim'] = 'width';
                
        $this->image_lib->initialize($config);
                        
        $this->image_lib->resize();
        unset($config);            
        $this->image_lib->clear();

        //echo $this->image_lib->display_errors();                
        }
        //echo $this->upload->display_errors();                
    }
    
    function _create_thumb($file){
        if($file != '')
        {
        $this->setMemoryForImage($file['full_path']);
        $config['image_library'] = 'GD2';
        $image_base = explode(".", $file['full_path']);
        $thumb = $image_base[0].'_thumb.'.$image_base[1];
        $config['new_image'] = $thumb;
        $config['source_image'] = $file['full_path'];
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 75;
        $config['height'] = 75;
        $config['master_dim'] = 'width';                
        $this->image_lib->initialize($config);                
        $this->image_lib->resize();    
        unset($config);            
        $this->image_lib->clear();    
        //echo $this->image_lib->display_errors();                
        }
        //echo $this->upload->display_errors();
    }
    
    // --------------------------------------------------------------------


function setMemoryForImage( $filename ){
                
                $imageInfo = getimagesize($filename);
                $memoryLimitMB = 0;
    $MB = 1048576;  // number of bytes in 1M
    $K64 = 65536;    // number of bytes in 64K
    $TWEAKFACTOR = 20;  // Or whatever works for you
    $memoryNeeded = round( ( $imageInfo[0] * $imageInfo[1]
                                           * $imageInfo['bits']
                                           * $imageInfo['channels'] / 8
                             + $K64
                           ) * $TWEAKFACTOR
                         );
    //ini_get('memory_limit') only works if compiled with "--enable-memory-limit" also
    //Default memory limit is 8MB so well stick with that.
    //To find out what yours is, view your php.ini file.
    $memoryLimit = 8 * $MB;
    if (function_exists('memory_get_usage') &&
        memory_get_usage() + $memoryNeeded > $memoryLimit)
    {
        $newLimit = $memoryLimitMB + ceil( ( memory_get_usage()
                                            + $memoryNeeded
                                            - $memoryLimit
                                            ) / $MB
                                        );
        ini_set( 'memory_limit', $newLimit . 'M' );
        return true;
    }
    else{
        return false;
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB