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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.