Why doesn't setImageInterlaceScheme() make a jpeg progressive?

800 views Asked by At

I'm using php Gmagick to modify images. The following code works as expected except that the images are not progressive. Why? According to the GraphicsMagick docs it should. For reference, the input image is 666 x 1000.

    $img = new Gmagick();
    $img->setSize(900, 900)
        ->readImageBlob($image->getBytes())
        ->setImageInterlaceScheme(Gmagick::INTERLACE_PLANE)
        ->setImageResolution(96, 96)
        ->setImageFormat('jpeg')
        ->setCompressionQuality(70)
        ->resizeImage(900, 1351, Gmagick::FILTER_UNDEFINED, 1);

Note that

$img->getImageInterlaceScheme() === Gmagick::INTERLACE_PLANE

does return true after setting it.

Edit

I've tried both the INTERLACE_LINE and INTERLACE_PLANE constants. With neither seeming to have an effect on the output.

4

There are 4 answers

1
Jonathan On BEST ANSWER

The original author created a bug on php.net (https://bugs.php.net/bug.php?id=66444), where the correct answer was eventually posted. You need to use the undocumented method:

->setInterlaceScheme(Gmagick::INTERLACE_LINE)

Instead of:

->setImageInterlaceScheme(Gmagick::INTERLACE_LINE)

This worked for me! For reference I am using PHP 5.4.20 with gmagick 1.1.7RC2 on top of GraphicsMagick 1.3.18.

6
JSuar On

The documentation you point to states:

Use Line to create an interlaced PNG or GIF or progressive JPEG image.

So, I think you should be setting the interlace to line.

->setImageInterlaceScheme(Gmagick::INTERLACE_LINE)

Note: I'm not sure if INTERLACE_LINE is an actual value. I assumed it was based on your code. Basically, try the line option.

3
Carlos Robles On

Have you tried calling setImageInterlaceScheme before anything else? i cannot find the code, but maybe when you read the bits it already compose the image and then the interlacings takes no place.

$img
    ->setImageInterlaceScheme(Gmagick::INTERLACE_PLANE)
    ->readImageBlob($image->getBytes())
    ->setSize(900, 900) 
    ->setImageResolution(96, 96)
    ->setImageFormat('jpeg')
    ->setCompressionQuality(70)
    ->resizeImage(900, 1351, Gmagick::FILTER_UNDEFINED, 1);

For sure the interlaceScheme has to be INTERLACE_PLANE, as you can read in the docs you already know http://www.graphicsmagick.org/GraphicsMagick.html#details-interlace

2
Gordo On

I've finally found the answer to this (using PHP IMagick) after struggling for weeks.

It turns out you have to set the image format to 'pjpeg' instead of just jpeg.

I have no idea why by by doing so my Images are correctly detect as progressive and render progressively in the browser.

I assume this will be the same for 'GMagick'

$im->setImageFormat('pjpeg')