PHP Imagick中的裁剪错误?

PHP Imagick中的裁剪错误?,php,imagick,Php,Imagick,我试图裁剪动画gif,在输出中我得到了相同大小的图像,但被裁剪了 许多空白处都用帆布填满了 例如,我制作了动画gif 600x100,但请求了100x100裁剪,在输出上我得到了600x100图像,带有裁剪图像和空白 有人知道这个问题的解决方案吗 $gif = new Imagick($s['src']); foreach($gif as $frame){ $frame->cropImage($s['params']['w'], $s['params']['h'], $s['para

我试图裁剪动画gif,在输出中我得到了相同大小的图像,但被裁剪了

许多空白处都用帆布填满了

例如,我制作了动画gif 600x100,但请求了100x100裁剪,在输出上我得到了600x100图像,带有裁剪图像和空白

有人知道这个问题的解决方案吗

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

ImageMagick通常有一个“页面”或工作区,类似于背景层。这听起来像是裁剪图像后留下的(之前我用命令行工具解决了一些合成和调整大小的行为,这让我很困惑…)

查看PHP手册页面,我看到以下评论:

克里斯蒂安·德宁-09-Apr-2010 10:57
裁剪gif图像时(我对jpg和png图像没有问题),画布不会被移除。请在裁剪的GIF上运行以下命令,删除空白:


我和你有同样的问题,我发现解决方法是使用coalesceimages函数

以下是使用Imagick在php中裁剪和调整动画gif的工作示例:

<?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);
?>
希望有帮助


Ps:对不起,我的英文是:)

你是在每一帧上还是在整张图片上都试过了?我的图像在只裁剪高度时降低了高度。这个把戏奏效了。在作物命令之前添加这一行。是的,我也这样做了。:)忘了在这里发帖了。无论如何,谢谢)coalesce是关键,这将失败,因为定义了$x或$y,但不是两者都定义了。
<?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);
?>
$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
}