View Full Version : Fatal error: Allowed memory size exhausted when resizing an image

01-02-2008, 12:26 PM

I'm trying to resize an uploaded image, but get the following error:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 3000 bytes) in /home/httpd/vhosts/crscuderia.com/httpdocs/crs/cms/uploadFile2.php on line xxx

And here's the script:


if ($_FILES['file']) {

$uploaddir = '/home/img/uploaded_images/large/';

for ($i=0; $i<count($_FILES['file']['type']);$i++) {
$uploadfile = $uploaddir.basename($_FILES['file']['name'][$i]);
$type = $_FILES['file']['type'][$i];
$tmp_name = $_FILES['file']['tmp_name'][$i];

if ($type=="image/jpeg" || $type=="image/pjpeg") {
//check if the file with the same name exists
if (!file_exists($uploadfile)) {
move_uploaded_file($tmp_name, $uploadfile);
//copy and resize
//copy file to 'medium' folder
$copydir1 = '/home/img/uploaded_images/medium/';
$newfile1 = $copydir1.basename($uploadfile);
copy($uploadfile, $newfile1) or die('Could not copy file '.basename($uploadfile).' to medium folder');

//resize new file
$im = @imagecreatefromjpeg($newfile1);
if ($im === false) {
die('unable to open image');

list($Width, $Height) = getimagesize($newfile1);
$NewWidth = 750;
$NewHeight = 500;

$ResizedImage = imagecreatetruecolor($NewWidth, $NewHeight);
imagecopyresampled($ResizedImage, $im, 0, 0, 0, 0, $NewWidth, $NewHeight, $Width, $Height) or die('image not resampled');

} else {
$errorstring .= 'File '.basename($uploadfile).' already exists<br>';
} elseif ($type == '') {
$errorstring .= '';
} else {
$errorstring .= 'File '.basename($uploadfile).' is not a valid file type<br>';

//display message to user
if ($errorstring != '') {
echo $errorstring;
echo '<a href="uploadFile1.php">Go back</a>';
} else {
echo 'File(s) uploaded.<br><a href="uploadFile1.php">Upload more files<a>';

Can anyone tell me what I'm doing wrong?


01-02-2008, 12:38 PM
Apparently, you're just running out of memory. I see you've tried to increase the limit to no avail b/c it still needs more. The imagecreatefromjpeg function creates a bitmap image in memory from the jpeg file. For every pixel in the image it will use at least 3 bytes, plus some overhead. Its trying to allocate another 3000 bytes thats not available.... for another pixel. How many pictures are you trying to resize at once? What happens when you try to resize one picture ( < 1-2MB)?

01-02-2008, 12:46 PM
I'm trying to resize just one picture, in order to test the script. My test image is a jpeg, 224 KB.
However, I still get the same error.