Page 1 of 1

cropImage produce different results for PNG and GIF format

Posted: 2009-05-11T15:22:15-07:00
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;
?>

Re: cropImage produce different results for PNG and GIF format

Posted: 2009-05-11T15:36:22-07:00
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;
?>

Re: cropImage produce different results for PNG and GIF format

Posted: 2009-05-14T12:28:16-07:00
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.

Re: cropImage produce different results for PNG and GIF format

Posted: 2009-05-14T13:33:28-07:00
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!