Crop bug in PHP Imagick?
I was trying to crop the animated gif and in the output i'm getting the same sized image, but cropped.
A lot of empty space is filled with canvas.
For example i had animated gif 600x100, but have requested 100x100 crop, on the output i'm getting 600x100 image with cropped image and empty space.
Someone knows the solution for this issue?
$gif = new Imagick($s['src']);
开发者_开发知识库
foreach($gif as $frame){
$frame->cropImage($s['params']['w'], $s['params']['h'], $s['params']['x'], $s['params']['y']);
}
$gif->writeImages($s['dest_path'] .'/'. $fullname,true);
I've been having the same problem as you, and I found the solution was using the coalesceimages function.
Here's a working example for crop and resize an animated gif in php with Imagick:
<?php
// $width and $height are the "big image"'s proportions
if($width > $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height > $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
$image = new Imagick(HERE_YOU_PUT_BIG_IMAGE_PATH);
$image = $image->coalesceImages(); // the trick!
foreach ($image as $frame) {
$frame->cropImage($width, $height, $x, $y); // You crop the big image first
$frame->setImagePage(0, 0, 0, 0); // Remove canvas
}
$image = $image->coalesceImages(); // We do coalesceimages again because now we need to resize
foreach ($image as $frame) {
$frame->resizeImage($newWidth, $newHeight,Imagick::FILTER_LANCZOS,1); // $newWidth and $newHeight are the proportions for the new image
}
$image->writeImages(CROPPED_AND_RESIZED_IMAGE_PATH_HERE, true);
?>
Code above is being used for generating thumbnails with same with and height. You might change it the way you want.
Notice that when using $frame->cropImage($width, $height, $x, $y); you should put there the values you might need.
IE $frame->cropImage($s['params']['w'], $s['params']['h'], $s['params']['x'], $s['params']['y']);
Of course that if you just want to crop instead of croping and resizing, just can do this:
$image = new Imagick(HERE_YOU_PUT_BIG_IMAGE_PATH);
$image = $image->coalesceImages(); // the trick!
foreach ($image as $frame) {
$frame->cropImage($s['params']['w'], $s['params']['h'], $s['params']['x'], $s['params']['y']);
$frame->setImagePage(0, 0, 0, 0); // Remove canvas
}
Hope it helps!
Ps: sorry for my english :)
Often ImageMagick has a 'page' or working area, something like a background layer. It sounds like this is remaining after cropping the image (I had a confusing time working out some compositing and resizing behavior with the command line tool before...).
Checking out the PHP manual page for cropImage, I saw this comment:
Christian Dehning - 09-Apr-2010 10:57
When cropping gif-images (I had no problems with jpg and png images), the canvas is not removed. Please run the following command on the cropped gif, to remove the blank space:
$im->setImagePage(0, 0, 0, 0);
精彩评论