Php Gdlib image resize memory?

282 Views Asked by At

I'm trying to upload and resize JPG photos. Once the photo is uploaded I resize it but get the following error:

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 15104 bytes)

Configuration (php.ini):

memory_limit = 64M

PHP :

ini_set("memory_limit", "64M");

What am I doing wrong?

3

There are 3 best solutions below

0
On

Let's do the conversion:

67 108 864 bytes = 65 536 kilobytes = 64 megabytes

67 123 968 bytes = 64 megabytes + 15 104 bytes (overlow)

Conclusion: 64M is not enough.

2
On

When resizing jpeg images with GD, the filesize often does not make as big of an impact on memory usage as the image dimensions. This is because jpegs have famously high compression and the GD functions, such as imagecreatefromjpeg will generate image/pixel data that is not compressed. You can probably avoid your error by trying one of at least a couple things:

  1. Increase the memory_limit even further
  2. Check the image dimensions right after upload and if the image's pixel count exceeds a certain amount, avoid further processing.
0
On

A decompressed image takes atleast 3 bytes per pixel so if you have really large images you can run into trouble. However it could also be your code that is at fault. Do not forget to call imagedestroy on any image you do not longer need in memory.