Crop bug in PHP Imagick?

寵の児 提交于 2019-12-23 19:02:05

问题


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);

回答1:


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 :)




回答2:


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);


来源:https://stackoverflow.com/questions/3950712/crop-bug-in-php-imagick

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!