Allowed memory size exhausted

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
hi there!

i save images in a mysql db and i wrote a script called getimage.php to
display them.
in my getimage.php i want to resize my images to display them as
thumbnails but there always occurs the following error:

<b>Fatal error</b>:  Allowed memory size of 188743680 bytes exhausted
(tried to allocate 240 bytes) in <b>.../public_html/getimage.php</b> on
line <b>x</b><br />

it always tries to allocate either 240 or 60 bytes.
line x equals: $image_p = imagecreatetruecolor($newWidth, $newHeight);

my configuration:

PHP Version 4.3.10
Apache/2.0.49 (Linux/SuSE)
GD Version: bundled (2.0.15 compatible)
memory_limit 180M !!!!!!!!

the machine has 512MB RAM.

I tried it with a jpg:
- the size was 163kb.
- the width was 533px
- the height was 400px

it was not a really big image but it also didn't worked with smaller

my code:
        include "inc/";
        $Main = new Main();
        $Database = new Database();
        $Database->SQLStatement = "SELECT ImgData, ImgType, ImgExt, ImgWidth,
ImgHeight FROM ProductPic WHERE ProductId=". $_REQUEST[ pid ];
        $Pic = $Database->Query();
                                $type = $Pic[ 0 ][ ImgType ];
        // set headers for image mime type
        Header( "Content-type: $type");

        // request thumbnail
        if ( $_REQUEST[ t ] == "tmb" )
            $newWidth = 60;
            $factor = $newWidth * $Pic[ 0 ][ ImgWidth ];
            $newHeight = $factor * $Pic[ 0 ][ ImgHeight ];

            // resample
            $image_p = imagecreatetruecolor($newWidth, $newHeight);
            $image = imagecreatefromstring( $Pic[ 0 ][ ImgData ] );
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newWidth,
$newHeight, $Pic[ 0 ][ ImgWidth ], $Pic[ 0 ][ ImgHeight ]);

            // output
            switch ( $Pic[ 0 ][ ImgExt ] )
                case 1:
                    imagegif($image_p, null, 75);
                case 2:
                    imagejpeg($image_p, null, 75);
                case 3:
                    imagepng($image_p, null, 75);
        else // request full size image
            $data = $Pic[ 0 ][ ImgData ];
            echo $data;

I know this problem if the memory limit is set to 8 MB but what the
hell is the solution here?

the free command on the linux-console returned:

             total       used       free     shared    buffers
Mem:        508588     122300     386288          0      14572
-/+ buffers/cache:      45780     462808
Swap:       497972          0     497972

kind regards

Re: Allowed memory size exhausted

Quoted text here. Click to load it

Take a look at imagedestroy():

You should use that on all images after you have finished using them, as it  
frees the memory used by/associated with that image.

One more note - why don't you cache your thumbnails?  That way you don't  
have to generate them every time.

(also, purely fyi, you might want to consider putting quotes around string  
keys in your arrays - PHP accepts it when you don't, but it's regarded as  
bad practice to do so)  

Re: Allowed memory size exhausted

thank you... the problem was that i just turned my brain on yet ;-)

the factor i used was: $factor = $newWidth * $Pic[ 0 ][ ImgWidth ];
but i want to resize, so the factor should be this: $factor = $newWidth
/ $Pic[ 0 ][ ImgWidth ];

after debugging my $factor the newWidth and newHeight parameters for
the imagecreatetruecolor command were correct!

Re: Allowed memory size exhausted

Quoted text here. Click to load it

Good work :)

Still - don't forget the imagedestroy() function, or caching your  
thumbnails - they'll save you a LOT of work down the road.


Site Timeline