私はあなたと同じ問題を抱えていました.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: 私の英語でごめんなさい :)