PHP: out of memory error, what to do?
I am receiving this error when Im trying to crop a image, I just uploaded. The image i just uploaded is around 3MB.
Here's the error
<br />
<b>Fatal error</b>: Out of memory (allocated 45088768) (tried to allocate 15552 bytes) <br />
An开发者_开发技巧d its happening when im trying to use this function: imagecreatefromjpeg($src);
I have at top:
ini_set('memory_limit', '256M');
So how can it be out of memory, and how should i solve this? My limit for a image upload standard 6MB. (Although it uploads fine to the server, but when i use crop, I get error on the line with imagecreatefromjpeg() function, and "out of memory..")
When doing image processing with GD, always free the used memory afterwards with imagedestroy
and do it as early as possible. So whenever you don’t need a image resource any more, destroy it with imagedestroy
immediately.
PHP usually allows you to change the memory_limit setting during runtime. There are some extensions though, like Suhosin, that will disable this behaviour. Check to see if your PHP setup has the Suhosin extension loaded using phpinfo(). If so, check to see if the *suhosin.memory_limit* setting is set to 0. If so, that would explain this behaviour (not updating memory_limit) and you'll have to contact the sysadmin to get that limit raised.
Also see http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit
A safe solution is to apply some filter which prevent image processing (or at least give some warning) when the process will need more memory than allowed limit. PHP manual on imgcreatefromjpeg have some user function (check on comment) which enable to calculate memory needed to process image. Based on the calculation, you may limit maximum size of uploaded image to prevent user triggering memory limit error.
note:don't forget to add few MB more on calculation.
But if you insist not to limit image size, try to ask your host to increase your memory limit or use third party service like Google Images Python Api.
精彩评论