Today I added a new feature in the content management system I'm building. Depending on where you are uploading an image to, PHP will resize the image to fit the designated location. It works quite well, but when I try to upload a larger image, as in a 3MB image, I'm getting a fatal error:
Fatal error: Allowed memory size of 134217728 bytes exhausted
(tried to allocate 42520 bytes) in...
I'm thinking 128MB of memory is quite a lot, considering I don't run that much... at least I don't think so. It tried to allocate another 42520 bytes for the resizing process, but failed.
My question is should I (A) increase the limit or (B) re-evaluate why I'm using so much memory in the first place? Is 128MB a good number or is it too large/too little?
Thanks,
Ryan
RESOLUTION
I concluded that 128MB is really too much for resizing an image and I was so focused at looking at other options... like exec() options, that I never took a closer look at my "sample" data. Turns out, that even though my large image was only 2.83MB, it was OVER 10000px wide. That's a problem. :)
GD stores images as bitmaps in memory, so I wouldn't completely rule out the possibility that with some JPG images (for example, high resolution, highly compressed), the bitmap version could be quite large.
Call memory_get_usage() right before you open & start resizing the image & see if that amount of memory is way too big.
Also just FYI, when the script says "(tried to allocate 42520 bytes)", that doesn't mean it just needed 42520 more bytes to run successfully. It just means at that moment it needed 42520 more bytes. A bit later it might have tried to allocate more memory. So, just adding 42520 more bytes to the memory total likely wouldn't have fixed anything.
How are you resizing the image? I hope you're using a library function for it like imagecopyresampled()
? If so, you shouldn't need 128M of RAM to resize a 3M image. It points to you doing something incorrectly or inefficiently.
You shouldn't ever need that much memory allocated. Re-evaluate your code to reduce it's consumption and make sure you aren't storing data superfluously.
Rough estimate of how much memory is consumed by a TrueColor image:
width x height x 4
If your users are informed about the maximum dimensions (in pixels) of an uploaded image, then you can determine the minimum RAM you have to allocate.
BTW, consider the variables being used in operations like format conversion, copying, etc.
With 64mb you have a lot to work with. If you need more you need something that starts cleaning up on used memory. 128Mb should be the max a script would need and that must be a very huge script indeed.