imagick
imagick copied to clipboard
Imagick::getImageGeometry() returning wrong dimensions
Hi,
I've been trying to debug in issue with resizing .gif images within our Drupal 9 site.
I've tracked this down to the Imagick extension reporting the wrong width / height for a gif image:
<?php
$image = new Imagick();
$image->readImage("/home/matt/Desktop/3-stylish-men-xmas.gif");
var_dump($image->getImageGeometry());
$ ~/Desktop php test.php
array(2) {
["width"]=>
int(375)
["height"]=>
int(452)
}
This reports the image is 375x452 px
when it's actually 800x550 px
:
I've seen this happening with a few gifs around the site (but not all, confusingly).
PHP 8.0.14 (cli) (built: Dec 20 2021 21:23:16) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.14, Copyright (c) Zend Technologies
with Zend OPcache v8.0.14, Copyright (c), by Zend Technologies
with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans
~/Desktop php -i | grep imagick
/etc/php/8.0/cli/conf.d/20-imagick.ini,
imagick
imagick module => enabled
imagick module version => 3.6.0
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
imagick.allow_zero_dimension_images => 0 => 0
imagick.locale_fix => 0 => 0
imagick.progress_monitor => 0 => 0
imagick.set_single_thread => 1 => 1
imagick.shutdown_sleep_count => 10 => 10
imagick.skip_version_check => 1 => 1
~/Desktop php -i | grep imagemagick
Imagick compiled with ImageMagick version => ImageMagick 6.9.11-60 Q16 x86_64 2021-01-25 https://imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.9.11-60 Q16 x86_64 2021-01-25 https://imagemagick.org
Are there any known work-arounds?
Ah I found a little more detail from the imagemagick CLI:
~/Desktop identify 3-stylish-men-xmas.gif
3-stylish-men-xmas.gif[0] GIF 800x550 800x550+0+0 8-bit sRGB 256c 0.000u 0:00.000
3-stylish-men-xmas.gif[1] GIF 376x458 800x550+3+59 8-bit sRGB 256c 0.000u 0:00.000
3-stylish-men-xmas.gif[2] GIF 353x466 800x550+26+59 8-bit sRGB 256c 0.000u 0:00.000
3-stylish-men-xmas.gif[3] GIF 375x452 800x550+3+59 8-bit sRGB 256c 459138B 0.000u 0:00.000
It seems that Imagick is only looking at the last frame?
Oh I found a workaround:
<?php
$image = new Imagick();
$image->readImage("/home/matt/Desktop/3-stylish-men-xmas.gif");
var_dump($image->coalesceImages()->getImageGeometry());
Calling coalesceImages()
first seems to return the correct dimenions.
Cool.
Yeah, I was about to suggest coalesceImages......and agree that this behaviour is pretty surprising.
I should probably make a list of 'gotchas' that people are going to encounter, somewhere.