Skip to content
Advertisement

imagecreatefromjpeg exhausted memory limit, but it’s wrong

I set the memory_limit in my PHP config files to 100MB. I have an image uploading script, that is working OK for some small images, but I tried to upload a relatively large image (13MB), and got the following error:

Allowed memory size of 104857600 bytes exhausted (tried to allocate 45056 bytes)

which points to this line: $image = imagecreatefromjpeg($file['tmp_name']);

What is the cause for it being exhausted if it’s much lower than the limit, ad it tries to allocate only 45056 Bytes, which are only 0.045 MB?

Thanks

Advertisement

Answer

It’s not wrong, you’re just misunderstanding what it’s telling you. It tries to allocate an additional 45056 bytes to whatever memory it already had, and that exceeded the limit of 104857600.

As for why an image uses that much memory: JPEG is a compressed storage format. In order to display it on screen, the computer needs to know each individual pixel. Each pixel has three colour values (in some image formats even more, plus perhaps a transparency layer) each typically 1 byte large (some formats use even more, others less). Given an image 5000 by 10000 pixels that’s:

5000 * 10000 * 3 / 1024 / 1024 = 143.0511474609 MB

To store all pixels in memory at once so it can manipulate or display the image, you need 143 MB of free RAM. Basically, convert the JPEG into a BMP and you know how much space it really takes.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement