cropImage produce different results for PNG and GIF format

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
woshiadai

cropImage produce different results for PNG and GIF format

Post by woshiadai »

Sample image: http://palmzlib.sourceforge.net/images/plogo.png

when you crop the image and output it as PNG and GIF, you get different results. PNG has the correct crop width and height, GIF still keeps the original canvas size, but shifts the cropped image.

Note that crop is done from (20, 0) in the example

Output PNG:

Code: Select all

<?php
$im = new Imagick('plogo.png');
$crop_w = 80;
$crop_h = 120;
$x = 20;
$y = 0;

$im->cropImage($crop_w, $crop_h, $x, $y);

$im->setImageFormat('png');
header('Content-Type: image/png');

echo $im;
?>
Output GIF:

Code: Select all

<?php
//$im = new Imagick('stock.png');
$im = new Imagick('plogo.png');
$crop_w = 80;
$crop_h = 120;
$x = 20;
$y = 0;

$im->cropImage($crop_w, $crop_h, $x, $y);

$im->setImageFormat('gif');
header('Content-Type: image/gif');

echo $im;
?>
Attachments
crop and output gif, not the expected result
crop and output gif, not the expected result
plogo-crop.gif (7 KiB) Viewed 11177 times
crop and output png, expected result
crop and output png, expected result
plogo-crop.png (8.8 KiB) Viewed 11177 times
original png
original png
plogo.png (8.19 KiB) Viewed 11177 times
woshiadai

Re: cropImage produce different results for PNG and GIF format

Post by woshiadai »

Just did a bit research and seems like setImagePage does the trick to reposition the cropped image. But still, this seems to be a bug :shock:

Code: Select all

<?php
$im = new Imagick('plogo.png');
$crop_w = 80;
$crop_h = 120;
$x = 20;
$y = 0;

$im->cropImage($crop_w, $crop_h, $x, $y);

$im->setImagePage($crop_w, $crop_h, 0, 0); // reposition

$im->setImageFormat('gif');
header('Content-Type: image/gif');

echo $im;
?>
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: cropImage produce different results for PNG and GIF format

Post by mkoppanen »

This seems to happening mainly with GIF images and I am not fully sure why it happens. It has something to do with virtual canvas on GIFs. On the other hand the virtual canvas might be something that someone might want to use so it would not be polite to call MagickSetImagePage automatically during crop/resize.
Mikko Koppanen
My blog: http://valokuva.org
woshiadai

Re: cropImage produce different results for PNG and GIF format

Post by woshiadai »

Hi, Mikko

Thanks for the great work on the Imagick.

Can you please elaborate a bit on the reply? It seems this problem only happens when I do the crop and set the output to gif. Do you mean that by calling setPage on other formats like jpg and png, there will be side effects to the virtual canvas? I think the virtual canvas will be resized after the crop anyways, not true?

Thanks!
Post Reply