Memory Limit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikelbring
Forum Commoner
Posts: 38
Joined: Sat Jan 05, 2008 5:28 pm

Memory Limit

Post 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.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Memory Limit

Post 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?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Memory Limit

Post by omniuni »

add to the top of your file:

Code: Select all

<?php ini_set("memory_limit","20M"); ?>
Increase the memory size as needed.
mikelbring
Forum Commoner
Posts: 38
Joined: Sat Jan 05, 2008 5:28 pm

Re: Memory Limit

Post 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.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Memory Limit

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Memory Limit

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
mikelbring
Forum Commoner
Posts: 38
Joined: Sat Jan 05, 2008 5:28 pm

Re: Memory Limit

Post 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.
Post Reply