Page 1 of 1

Memory Limit

Posted: Tue Jan 06, 2009 9:44 pm
by mikelbring
I tried to run a php thumbnailer function on a 4MB image and I kept getting the memory limiter. I know the function works because I have used it with various smaller images. I have tried about everything from changing the php.ini, I was doing this on my local server.

Here is my image function if it helps:

Code: Select all

public function buildThumbnail($source,$thumb,$maxsize,$s = false,$exit = true){
    
        #See if a valid image is being handled.
        if($s === false) {
        
            if (!$s = getimagesize($source)) exit("Could not get image's size.");
            
        }
        if(!$s) endpage("Invalid image file.",1);
        #Check the thumbnails width and height
        if ($s[0] <= $maxsize && $s[1] <= $maxsize) {
        
            $w = $s[0];
            $h = $s[1];
            return true;
                
        } else {
        
            $xy = $maxsize;
            $ss = min($xy/$s[0],$xy/$s[1]);
            $w = (int)($s[0]*$ss);
            $h = (int)($s[1]*$ss);
            
        }
 
        #Depending on the mime of the image, create a new one of that type.
        switch ($s['mime']) {
            case 'image/jpeg':$imagecreate='imagecreatefromjpeg';break;
            case 'image/gif':$imagecreate='imagecreatefromgif';break;
            case 'image/png':$imagecreate='imagecreatefrompng';break;
            default:exit("The image must be jpeg,jpg,gif or png.");break;
        }
        $image = strval(str_replace('/',null,$s['mime']));
        #Set aside some space for this image
        if(!$r = imagecreatetruecolor($w,$h)) exit("Could not create a new image.");
        $t = $imagecreate($source);
        imagecopyresampled($r,$t,0,0,0,0,$w,$h,$s[0],$s[1]);
        #Put some color in one of the pre allocated spaces
        $border = ImageColorAllocate($r, 108, 108, 108);
        #Save the image
        $w = $w - 1;
        $h = $h - 1;
        
        $re = $image($r,$thumb,100);
        #Clean up
        imagedestroy($t);
        imagedestroy($r);
        @chmod($thumb,0777);
        return $re;
        
    }
Any help would be appreciated.

Re: Memory Limit

Posted: Tue Jan 06, 2009 11:10 pm
by it2051229
hmmmmm.... yeah the PHP.ini..... but how come?, have you tried restarting your server after modifying the PHP.ini?... orrr maybe you're configuring the wrong PHP.ini file.. by the way, what application are you using to host your php?

Re: Memory Limit

Posted: Wed Jan 07, 2009 1:43 am
by omniuni
add to the top of your file:

Code: Select all

<?php ini_set("memory_limit","20M"); ?>
Increase the memory size as needed.

Re: Memory Limit

Posted: Wed Jan 07, 2009 10:30 am
by mikelbring
Did what you said and did not get much success

Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 14592 bytes)

I am using xampp, but I have tried it on a standard linux server as well.

Re: Memory Limit

Posted: Wed Jan 07, 2009 10:42 am
by omniuni
Odd as it may sound, check that your permissions are correct on the file. I've gotten this error before, and it was something funny like that.

Re: Memory Limit

Posted: Wed Jan 07, 2009 10:50 am
by VladSun
mikelbring wrote:Did what you said and did not get much success

Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 14592 bytes)

I am using xampp, but I have tried it on a standard linux server as well.
20MB = 20*1024*1024 B = 20971520

as omniuni said: "Increase the memory size as needed."

4MB compressed image != 4MB bitmap image

I mean that imagecreate*() function generates an uncompressed image in memory.

Re: Memory Limit

Posted: Wed Jan 07, 2009 10:56 am
by mikelbring
So how would I make it generate a compressed. I am resizing a large 10 megapixel image to 750 KB. I would not think it would still be at 4MB.

Scratch that. I was able to get it. It made it only about 250kb. Thanks.