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?