1

I have a gallery of JPEG images I'm trying to manage using PHP's GD. One particular JPEG image is giving me trouble. It identifies itself with dimensions of 32,768 x 1,024 pixels. The image is only 1.9 MB on disk. It's handled fine by other image processing tools like Finder and Preview on my Mac, and ImageMagick. Yet, when my system calls imagecreatefromjpeg() on it, I get a classic "Allowed memory size exhausted" fatal exception. I believe the image is corrupted. It's supposed to be a 1024x1024 snapshot of a web page, created with wkhtmltoimage.

Ordinarily the answer to this is to increase PHP's memory_limit. But mine is already big, at 256MB.

Is there anything I can do to preemptively detect this type of image corruption and gracefully handle it? If I add an "@" before the imagecreatefromjpeg() call, PHP merely dies with "500 Internal Server Error" instead. I can't use try/catch either since it's a fatal error.

FWIW, here's how ImageMagick's identify tool describes it:

myimage.jpg JPEG 32768x1024 32768x1024+0+0 8-bit sRGB 1.948MB 0.000u 0:00.000

I suppose I could do if ($width == 32768) { ... }, but that's hackish. There could be an image with that width.

Any other ideas?

curtisdf
  • 4,130
  • 4
  • 33
  • 42
  • Are you sure this actually corrupt? Its possible that an image could actually have those dimensions and be that size. JPEG uses lossy compression. – datasage Feb 25 '13 at 20:51
  • The images come from `wkhtmltoimage`. They are snapshots of web pages. The input parameters I'm using tell it to produce a 1024x1024 output. So maybe they're not "corrupt" from a binary data standpoint, but the output of `wkhtmltoimage` is incorrect at least. – curtisdf Feb 25 '13 at 21:15
  • For now, I've edited my web page snapshot generator to double-check that the dimensions of the images it creates are correct. But I'd still like to know if there's a way to preemptively avoid the out-of-memory error if the desired dimensions aren't known ahead of time. – curtisdf Feb 25 '13 at 21:17
  • I would probably try to chase down why the image is being generated with such a large width, rather than try to compensate for it later. – datasage Feb 25 '13 at 21:19

0 Answers0