我遇到了和你一样的问题,我发现解决方案是使用 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);
?>
上面的代码用于生成具有相同高度和高度的缩略图。
你可以随意改变它。
注意当使用 $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
}
希望对你有帮助!
Ps:对不起我的英语:)