在PHP Imagick中裁剪错误? [英] Crop bug in PHP Imagick?
问题描述
我试图裁剪动画gif,在输出中我得到了相同大小的图像,但是被裁剪了.
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.
例如,我将gif动画设置为600x100,但要求进行100x100的裁剪,在输出中我得到的是600x100的图像,其中包含裁剪的图像和空白区域.
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);
推荐答案
我遇到了与您相同的问题,发现解决方案使用了coalesceimages函数.
I've been having the same problem as you, and I found the solution was using the coalesceimages function.
这是一个使用Imagick在php中裁剪和调整动画gif大小的工作示例:
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.
注意,当使用$ frame-> cropImage($ width,$ height,$ x,$ y);您应该将可能需要的值放在此处.
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']);
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
}
希望有帮助!
Ps:对不起,我的英语:)
Ps: sorry for my english :)
这篇关于在PHP Imagick中裁剪错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!