PHP Imagick中的作物错误?

Som*_*ody 3 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['params']['x'], $s['params']['y']);            
}   

$gif->writeImages($s['dest_path'] .'/'. $fullname,true);
Run Code Online (Sandbox Code Playgroud)

Nic*_*las 6

我遇到了和你一样的问题,我发现解决方案是使用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);
?>
Run Code Online (Sandbox Code Playgroud)

上面的代码用于生成具有相同和高度的缩略图.您可以按照自己的方式进行更改.

请注意,当使用$ frame-> cropImage($ width,$ height,$ x,$ y)时; 你应该把你可能需要的值放在那里.

IE $ frame-> cropImage($ s ['params'] ['w'],$ s ['params'] ['h'],$ s ['params'] ['x'],$ s [' PARAMS '] [' Y']);

当然,如果你只想裁剪而不是裁剪和调整大小,那么就可以这样做:

$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
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!

Ps:对不起我的英文:)


JAL*_*JAL 5

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

查看cropImage的PHP手册页,我看到了这个评论:

Christian Dehning - 2010 年 4 月 9 日 10:57
裁剪 gif 图像时(我对 jpg 和 png 图像没有问题),画布不会被移除。请在裁剪后的 gif 上运行以下命令,以删除空格:

$im->setImagePage(0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)