I've got an older piece of code that attempts to load an image file. Since it doesn't know what type it is, the code tries them all:
$res = @imagecreatefromjpeg($sourceName);
if ( $res === false)
$res = @imagecreatefromgif($sourceName);
if ( $res === false)
$res = @imagecreatefrompng($sourceName);
if ( $res === false) {
$err[] = 'Cannot load file as JPEG, GIF or PNG!';
This used to work perfectly for a long time, but after a recent server migration it stopped working when using a PNG file (JPEG files work fine). Investigation revealed that the very first line produces a fatal error:
Fatal error: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: Not a JPEG file: starts with 0x89 0x50
Since the line has the error-suppression operator, the script simply stopped executing and returned HTTP 200 with an empty response.
What I don't understand is... wha... how... why... since when... huh?
Even the official documentation says that these functions return FALSE
when unable to load a file, and the construct with the @
operator is shown there too. I can find nothing on the web which would say that "not a JPEG file" is a fatal error. Everywhere it's mentioned, it's a warning as it always has been. Why am I getting a fatal error now?
My PHP is 5.6.30, GD is there with everything.
I guess I can answer my own question. It's a known bug. See. PHP bugs #73514 and #73479. And actually the bug is in libgd, and has been reported there too. A fix was made last November, but still hasn't made it to a release. So, yeah... no luck. I'll try to use getimagesize()
instead to determine the type first and use the appropriate function instead. That still won't help in the case of a genuinely corrupt file, but it's something.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With