Support»Allowed Memory Size

Allowed Memory Size

Converting (resampling) an image larger than a certain size can cause PHP to exceed its allowable memory limit. Depending on how PHP's error reporting is configured, you may see the following:

Fatal error: Allowed memory size of XXXXXXXX bytes exhausted (tried to allocate XXXX bytes) in /path/to/gallery/index.php on line XXX

This happens because converting a compressed image requires the image to be held in memory, uncompressed, at the original size and new size simultaneously. 10:1 compression is not unusual for JPEG images, so this may require more memory than you might expect.

Depending on your circumstances, some solutions might be

  • Increase the memory size limit by editing php.ini and restarting the server.
  • Reduce the size of your full-size images.
  • Change the image conversion method.
  • Do resampling on a separate development machine.


My 2-megapixel Olympus digicam takes 1600x1200 x 24-bit-per-pixel pixel images. For the color depth let's call it "three bytes" instead of "24 bits" per pixel. When the digicam is set at the default "High" quality level, an average-sized image occupies about 500K of storage space.

Despite only being "0.5 MB images", each image requires

 3 x 1600 x 1200 = 5,760,000 bytes = 5.76 MB = 5.49 MiB

in memory.

Using this as a guide, if your images are using the same level of jpeg compression (probably not, but you can do the math), then a 2 MB image might need over 20 MB(!) if memory.

With this in mind, take a look a the GD code for converting a JPEG image:

$src_img = imageCreateFromJpeg($orig_img);
$dst_img = imageCreateTruecolor($dest_width, $dest_height);
imageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width,
  $dest_height, $src_width, $src_height);
imageJpeg($dst_img, $cnvrtd_img, $cnvrt_thmb['qual']);

Note that two copies of the 3 byte-per-pixel image must occupy memory simultaneously; one at the original size and one at the resampled size. This memory needs to be available in addition to other memory needed by the PHP script at the time.

If all else fails, you can develop your galleries on a more powerful development machine, perhaps a desktop computer, then transfer the completed galleries to the resource-challenged machine. This has other benefits, such as

  1. image conversion software isn't even necessary on the production server, and
  2. galleries produced in this manner are read-only on the production server, which is a security advantage.