dangel dangel - 1 month ago 15x
PHP Question

GD (PHP) failing with bad JPEG image resize (Laravel Intervention\Image), no error in error.log

I'm running Apache/PHP 5.6 on both a Windows and Ubuntu dev box. Using Laravel 5.1 and Intervention/Image with the GD driver, I'm trying to resize some pretty large images (15-25MB) the problem is the resize is failing with the error

Unable to read image from file (/tmp/phpxxxxxx)
, but only on large images. Anything in the 5-10MB range has no problem resizing...

I'm assuming it has something to do with the process running out of memory (as smaller files have no issue), however the problem is I'm not actually getting any error messages at all in error.log...

I've increased
in my php.ini to 2000M for testing, but the resize still fails.

The code in the controller to resize the image is...

$img = Img::make($file->getRealPath());

The details of the error reported through Laravel are below...

NotReadableException in Decoder.php line 46:
Unable to read image from file (C:\Apache24\htdocs\tmp\php361F.tmp).
in Decoder.php line 46
at Decoder->initFromPath('C:\Apache24\htdocs\tmp\php361F.tmp') in AbstractDecoder.php line 293
at AbstractDecoder->init('C:\Apache24\htdocs\tmp\php361F.tmp') in AbstractDriver.php line 64
at AbstractDriver->init('C:\Apache24\htdocs\tmp\php361F.tmp') in ImageManager.php line 50
at ImageManager->make('C:\Apache24\htdocs\tmp\php361F.tmp') in Facade.php line 216
at Facade::__callStatic('make', array('C:\Apache24\htdocs\tmp\php361F.tmp')) in SubmitPhotoController.php line 97
at Image::make('C:\Apache24\htdocs\tmp\php361F.tmp') in SubmitPhotoController.php line 97
at SubmitPhotoController->store()

How would I go about further troubleshooting to find out why this is failing?


  1. I've decreased
    just to make sure a low value would show an error when resizing and it does, so I don't think it's a PHP memory issue.

  2. I DO see the file showing up in C:\Apache24\htdocs\tmp before quickly going away.


upload_max_filesize = 2000M
post_max_size = 2000M

After doing some testing, the images that were failing were those that were created by the panorama feature on my cell phone. Even smaller file sized images ended up failing.


GD needed to be told to ignore warnings from bad JPEG as apparently GD isn't very tolerant of faulty or broken JPEGs. These images that were failing were actually created on my phone using the built in panorama tool. Large images downloaded online or open\saved through photoshop (repaired) were actually just fine after I started testing.

ini_set("gd.jpeg_ignore_warning", 1);

http://stackoverflow.com/a/3901885/895810 and https://bugs.php.net/bug.php?id=39918.

The problem stemmed from the following lines of Intervention\Image\Gd\Decoder.php

$info = @getimagesize($path);

// define core
        switch ($info[2]) {
            case IMAGETYPE_PNG:
                $core = @imagecreatefrompng($path);

            case IMAGETYPE_JPEG:
                $core = @imagecreatefromjpeg($path); //<--------here

I could imagine running into other issues though by completely ignoring warnings.