View Full Version : imagecreatefromjpeg() using far too much memory for what is needed

Satal Keto
11-18-2007, 01:30 PM
I am creating a website for a friend of mine who is a model.
One of the things that she would like for the website to do, is to allow her to choose a file that she wants on the website and then for using a simple form on the website to upload the file and then make a thumbnail of that picture.

The code I am using for the creation of the thumbnail is...

function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbHeight)
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);

$ratio = $origHeight / $thumbHeight;
$thumbWidth = $origWidth / $ratio;
imagejpeg($dst_img,$thumbDirectory . "/" . $imageName);

When I am running the script I am recieving the error message

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 9792 bytes) in /home/fhlinux159/s/sarah.satalketo.co.uk/user/htdocs/adminPhotos.php on line 5
(Line 5 being "$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");")
Now considering the original file was 426KB I think it using up 32MB is a little silly.
So I was wondering whether I have made an error somewhere and this is causing the massive usage of memory.

11-18-2007, 04:24 PM
Jpg/jpeg/gif/png images are compressed. To be able to operate on the images and do things like add text, draw on them, crop, rotate, save or output them in other formats the GD imagecreate.... functions uncompress the image data into a bitmap image.

From the GD section of the manual -
Image functions are very memory intensive. Be sure to set memory_limit high enough.

The total memory used is everything your program consumes for php code and data. If your code is looping over a number of images or has a lot of resources allocated for arrays or database result sets, you can probably free up some memory. Otherwise, you will need to increase the amount of memory available to php - see the memory_limit setting.