Search code examples
phpsymfonymamp

Error Gd Libraries in Symfony


I Want to use gd Library with symfony but I have this error

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error:

My script in my Controller is

if (extension_loaded('gd') && function_exists('gd_info')) {
            echo "PHP GD library is enabled in your system.";
        }
        else {
            echo "PHP GD library is not enabled on your system.";
        }

        echo phpinfo();

        $filename = $this->get('kernel')->getRootDir() . '/../web/logo/logo.png';
        $percent = 0.5;

// Content type
//        header('Content-Type: image/jpeg');

// Get new sizes
        list($width, $height) = getimagesize($filename);
        $newwidth = $width * $percent;
        $newheight = $height * $percent;

// Load
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromjpeg($filename);

// Resize
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
        imagejpeg($thumb);

        return new Response("foo");

In my Phpinfo I can see GD libraries.

I'm using Mamp 3.2.1


Solution

  • This is a known problem with GD library and not related to Symfony. GD is very strict when it comes to the format of JPEG files, as it only accepts a very specific JPEG configuration.

    There are many resources on this problem. E.g. http://php.net/manual/de/function.imagecreatefromjpeg.php#55402

    If you get this error: "Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error" then check the JPEG files. If they are saved in CMYK format (instead of RGB) then GD will fail to load them (tested with GD 2.0.12)

    the dreaded "Warning: imagecreatefromjpeg() : '/tmp/filename' is not a valid JPEG file in /phpfile.php on line xxx"

    It seems that the GD library is less tolerant of buggy JPEG files than other programs. The solution suggested was to set GD to ignore JPEG error's before processing the image, like this:

    ini_set("gd.jpeg_ignore_warning", 1);