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.