3

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.

Isius
  • 6,712
  • 2
  • 22
  • 31

4 Answers4

2

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.

Jonathan
  • 123
  • 1
  • 9
  • You just saved my day. The function setInterlaceScheme works but the function setImageInterlaceScheme does not. On the other hand, to get the interlace scheme, getImageInterlaceScheme works but getInterlaceScheme does not. It's weird. – user3032481 Dec 23 '18 at 00:15
0

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.

JSuar
  • 21,056
  • 4
  • 39
  • 83
  • 1
    Good catch! I had tried both INTERLACE_LINE and INTERLACE_PLANE. Neither produces progressive jpegs. I've edited the question. – Isius Dec 24 '13 at 19:48
  • Try setting it to none, then separately setting it to plane or line so the interlace option is not applied when all the other options are applied. – JSuar Dec 28 '13 at 15:51
  • Tried that. No change. – Isius Dec 28 '13 at 18:38
  • I confirmed that the Line option does work with the graphicsmagick gm utility working with a JPEG. Line sets the interlace field to Line. Any other value will set it to No. Have you confirmed the output image using another tool just to confirm the current interlace value? – JSuar Dec 28 '13 at 19:17
  • Yessir, I've confirmed with multiple tools that the image is not interlaced. I'm starting to wonder if setImageInterlaceScheme isn't fully or perhaps properly implemented. – Isius Dec 28 '13 at 20:31
  • 1
    Just checked the source and it appears to be implemented. It relies on [MagickSetImageInterlaceScheme](http://www.magickwand.org/MagickSetImageInterlaceScheme.html). Maybe the interlace values are mapped wrong. Not sure. – JSuar Dec 28 '13 at 20:38
0

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

Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
0

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')
Gordo
  • 779
  • 9
  • 21
  • That's a shame... Have you tried it anyway? I couldn't find it mentioned anywhere in the IMagick documentation and just tried it on a whim and it worked. – Gordo Feb 11 '14 at 20:11
  • Yep. When I tried it it just produced no image at all. Same as if I try any other unsupported file format. – Isius Feb 11 '14 at 20:15