Jump to content

createimagefromjpeg() takes up too much memory???


bausman480

Recommended Posts

Hi,

 

So I have a script that is supposed to crop and resize an uploaded image. One of the lines is using createimagefromjpeg(), and this line is exceeding the allotted memory limit on my shared host: "Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 10368 bytes)".

 

This only happens whenI upload files larger than 1.5MB. I need my script to work with images up to 3-4MB. I spoke to my host and they cannot increase the memory limit. Here is the resize function that I'm using. Help?

 

<?php function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
$source = imagecreatefromjpeg($image); // THIS is the line causing the memory error
imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
imagejpeg($newImage,$thumb_image_name,90);
chmod($thumb_image_name, 0777);
return $thumb_image_name;
}?>

 

Help? How do I work with files largerr than 1.5MB? Is there another command that can replace imagecreatefromjpeg() that uses memory more efficiently?

Thank you!

You could try using a different library such as [m=imagick]ImageMagick[/m] or maybe GMagick if your host has them available.  Even if they don't have the ImageMagick extension they might have the cli apps which you can use exec to run separate from your script which would offload the memory usage to another process.

 

You'll have to check with your host to see what options they have available for you to use.  Running a separate program using exec is the only way to really reduce the memory usage from PHP's point of view though.  Switching to another library within PHP might make small differences but ultimately processing images is just a memory intensive process and there is not much you can do about it.

Thank you.

 

So as far as I understand, switching to another image library won't help too much.. So running an external program is I guess the only option.

How do you write this external program though? What language, can it still be php? Can I simply put the function above in a separate file and call it via exec?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.