By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

Allowed memory size exhausted

P: n/a
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
ones.

my code:
include "inc/Main.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);
break;
case 2:
imagejpeg($image_p, null, 75);
break;
case 3:
imagepng($image_p, null, 75);
break;
}
}
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
cached
Mem: 508588 122300 386288 0 14572
61948
-/+ buffers/cache: 45780 462808
Swap: 497972 0 497972
kind regards
flo

Feb 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
d
<f.*****@rona.at> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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
ones.

my code:
include "inc/Main.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);
break;
case 2:
imagejpeg($image_p, null, 75);
break;
case 3:
imagepng($image_p, null, 75);
break;
}
}
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
cached
Mem: 508588 122300 386288 0 14572
61948
-/+ buffers/cache: 45780 462808
Swap: 497972 0 497972
kind regards
flo


Take a look at imagedestroy():

http://www.php.net/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)
Feb 9 '06 #2

P: n/a
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!

Feb 9 '06 #3

P: n/a
d
<f.*****@rona.at> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
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!


Good work :)

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

dave
Feb 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.