Welcome Guest, Not a member yet? Register   Sign In
Image Resize doesn't work if the image is too big
#1

[eluser]megabyte[/eluser]
Anyone have issues using the image library for resizing? As far as resizing large images just under 2mb?

If so what solutions do we have?
#2

[eluser]CI Lee[/eluser]
What kind of errors are you getting?

Could be your servers upload limit for php or memory limit being exceeded.
#3

[eluser]xwero[/eluser]
If the upload file is too big you can add size validation.
#4

[eluser]megabyte[/eluser]
OK,

for those who are having this aame issue I found a solution. I didn't create it but I did find it for us all, lol.

If you are trying to resize images that are large, then you need to increase the memory usage like others have noted.

Well here's a function that calculates the amount of memory you need and increases it for you. Not sure if it works on all servers, but it did for me.

Code:
function setMemoryForImage( $filename ){
    $imageInfo = getimagesize($filename);
    $memoryLimitMB = 0;
    $MB = 1048576;  // number of bytes in 1M
    $K64 = 65536;    // number of bytes in 64K
    $TWEAKFACTOR = 8;  // 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;
    }
}
$filename being the full path to image
#5

[eluser]xwero[/eluser]
And the code works with smaller images?
#6

[eluser]megabyte[/eluser]
you worried me there, with your "smaller images" comment, lol.

I just tested it, and yes it works.
#7

[eluser]megabyte[/eluser]
here's the code I'm using.

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

                $config['upload_path'] = './photos/';
                $config['allowed_types'] = 'jpg|jpeg|gif|avi';
                $config['encrypt_name'] = 'TRUE';
                $this->upload->initialize($config);
                
                $this->upload->do_upload();
                $resarr = $this->upload->data();
                $file = $resarr['file_name'];
                $this->setMemoryForImage($resarr['full_path']);
                
                if($file != '')
                {
          
                $config['image_library'] = 'GD2';
                //$config['create_thumb'] = TRUE;
                $config['source_image'] = $resarr['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();
                        
                echo $this->image_lib->display_errors();
                    
                }
                echo $this->upload->display_errors();
            
            //print_r($resarr);        
        }
    }


function setMemoryForImage( $filename ){
    $imageInfo = getimagesize($filename);
    $memoryLimitMB = 0;
    $MB = 1048576;  // number of bytes in 1M
    $K64 = 65536;    // number of bytes in 64K
    $TWEAKFACTOR = 8;  // 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;
    }
}


I hope this helps out others like its helped out me.
#8

[eluser]antonumia[/eluser]
bloody great!
thanks, a




Theme © iAndrew 2016 - Forum software by © MyBB