Page 1 of 1

PHP Memory limit exhausted

Posted: Tue Oct 21, 2008 10:28 am
by mark.disney
Hello,
I keep getting this error is some code I have written, I have a form which allows the user to upload six images. These images are then uploaded to the server as three different sizes. The first image on the form gets uploaded three times to show a main product images and then a thumbnail and a bigger version of the thumbnail. The remaining the five get uploaded twice. Once as a thumbnail and the second as the bigger version.

I cannot seem to figure out what the problem is? The image size's which I am uploading are only 800kb???

Here is my code: (The snippet of code is what my form posts to)
---------------------------------------------------------------------------------------

$i=1;
while ($i < 7)
{
$photo = "";
$Photo = "Photo".$i;

if($_FILES['Photo'.$i]['name'] != "")
{
if (isset($_POST['update']))
{
$image_id = $String_prodid;
}
else
{
$image_id = mysql_insert_id();
}

$String_Photo = '../../images/products/photos/' . $image_id . '.jpg';
$String_Thumbnail = '../../images/products/thumbnail/' . $image_id . '_tmb.jpg';
$String_extras = '../../images/products/extras/' . $image_id . '_extra_'.$i.'.jpg';
$String_large = '../../images/products/large/' . $image_id . '_large_'.$i.'.jpg';


//add @ symbol back in
if(move_uploaded_file($_FILES[$Photo]['tmp_name'], $String_Photo))
{
// Creates the photo objects
list($Integer_OriginalWidth, $Integer_OriginalHeight) = getimagesize($String_Photo);
$Object_Original = imagecreatefromjpeg($String_Photo);

if ($i == 1)
{
// Creates the Thumbnail image, for id 1 only (main image)
$ratio_Width = 185 / $Integer_OriginalWidth;
$ratio_Height = 150 / $Integer_OriginalHeight;

if ($ratio_Width < $ratio_Height)
{
$scaled_Width_Thumbnail = $Integer_OriginalWidth * $ratio_Width;
$scaled_Height_Thumbnail = $Integer_OriginalHeight * $ratio_Width;
$Object_Thumbnail = imagecreatetruecolor($scaled_Width_Thumbnail, $scaled_Height_Thumbnail);
}
else if($ratio_Width > $ratio_Height)
{
$scaled_Width_Thumbnail = $Integer_OriginalWidth * $ratio_Height;
$scaled_Height_Thumbnail = $Integer_OriginalHeight * $ratio_Height;
$Object_Thumbnail = imagecreatetruecolor($scaled_Width_Thumbnail, $scaled_Height_Thumbnail);
}
imagecopyresampled($Object_Thumbnail, $Object_Original, 0, 0, 0, 0, $scaled_Width_Thumbnail, $scaled_Height_Thumbnail, $Integer_OriginalWidth, $Integer_OriginalHeight);
imagejpeg($Object_Thumbnail, $String_Thumbnail, 90);
}
// Creates the Large images, these are the larger imaegs that appear on rollover from images on right
$ratio_Width = 490 / $Integer_OriginalWidth;
$ratio_Height = 325 / $Integer_OriginalHeight;

if ($ratio_Width < $ratio_Height)
{
$scaled_Width_Large = $Integer_OriginalWidth * $ratio_Width;
$scaled_Height_Large = $Integer_OriginalHeight * $ratio_Width;
$Object_Large = imagecreatetruecolor($scaled_Width_Large, $scaled_Height_Large);
}
else if($ratio_Width > $ratio_Height)
{
$scaled_Width_Large = $Integer_OriginalWidth * $ratio_Height;
$scaled_Height_Large = $Integer_OriginalHeight * $ratio_Height;
$Object_Large = imagecreatetruecolor($scaled_Width_Large, $scaled_Height_Large);
}
imagecopyresampled($Object_Large, $Object_Original, 0, 0, 0, 0, $scaled_Width_Large, $scaled_Height_Large, $Integer_OriginalWidth, $Integer_OriginalHeight);
imagejpeg($Object_Large, $String_large, 90);

// Creates the Extra images, listed as the six on the right
$ratio_Width = 100 / $Integer_OriginalWidth;
$ratio_Height = 100 / $Integer_OriginalHeight;

if ($ratio_Width < $ratio_Height)
{
$scaled_Width_Extras = $Integer_OriginalWidth * $ratio_Width;
$scaled_Height_Extras = $Integer_OriginalHeight * $ratio_Width;
$Object_Extras = imagecreatetruecolor($scaled_Width_Extras, $scaled_Height_Extras);
}
else if ($ratio_Width > $ratio_Height)
{
$scaled_Width_Extras = $Integer_OriginalWidth * $ratio_Height;
$scaled_Height_Extras = $Integer_OriginalHeight * $ratio_Height;
$Object_Extras = imagecreatetruecolor($scaled_Width_Extras, $scaled_Height_Extras);
}
imagecopyresampled($Object_Extras, $Object_Original, 0, 0, 0, 0, $scaled_Width_Extras, $scaled_Height_Extras, $Integer_OriginalWidth, $Integer_OriginalHeight);
imagejpeg($Object_Extras, $String_extras, 80);
}
}
$i++;

---------------------------------------------------------------------------------------

Any suggestions or help would be appreciated

Mark

Re: PHP Memory limit exhausted

Posted: Tue Oct 21, 2008 10:56 am
by onion2k
You need to free up memory as you're using it with imagedestroy().
The image size's which I am uploading are only 800kb???
800kb when it's compressed as a JPEG ... PHP has to decompress it to a bitmap in order to do anything with it. Assuming that's an image about 2560 by 1600 pixels that will actually take up around 15MB of RAM.

Re: PHP Memory limit exhausted

Posted: Tue Oct 21, 2008 11:19 am
by mark.disney
Thank you for your quick response,

That has fixed the problem.

Thank you again.

Mark