473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Allowed memory size exhausted

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
3 6666
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul | last post by:
Hi, I'm using TikiWiki and was using its file upload feature, trying to upload a 2MB file. I get this error: Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate...
2
by: Phil Powell | last post by:
Actually the client is saying it sometimes happens and sometimes doesn't happen, and when they refresh their screen it clears itself (I assume the memory clears). Here is line 1135: $result =...
0
by: Joe | last post by:
Hi, Now I am using Red Hat Linux 8.0 + squirrelmail as mail server. One day, I got the following warning: Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 35...
0
by: prabhjeet | last post by:
I have DB2 v9 trial version installed on Windows 2003 Server with 1GB RAM. I am able to connect remotely through an ODBC data source to the database, but when I try to connect to that data source...
1
by: Kimmo Laine | last post by:
Hi! We've encountered a strange problem concerning memory usage. In the previous install the maximum memory amount per page was limited to 8 MB and it was never reached. Now, after upgrading...
3
by: spereira | last post by:
I am running XAMPP for Mac OS X 0.6.1 ! And since it did not have PDF libarires I downloaded ezpdf from the...
7
by: Jim Carlock | last post by:
I'm filling a really large array. Is there a way to get by this error message without editing the php.ini? The array needs to get built once, then looped through to fill a data- base, and it will...
1
by: David d'Angers | last post by:
hi group: i've been struggling with this simple problem with dismay i need all the file names in a directory since the number of files in a directory is unknown, a friend here in the group...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.