先还原动画各帧
选择一用PHP中的Imagick模块
<?php
$image = new Imagick('old.gif');
$i = 0;
foreach ($image as $frame) {
$frame->writeImage('old_' . $i++ . '.gif');
}
?>
选择二用ImageMagick提供的convert命令
shell> convert old.gif old_%d.gif
GIF动画为了压缩 会以第一帧为模板 其余各帧按照适当的偏移量依次累加
并只保留不同的像素 结果是导致各帧尺寸不尽相同 为缩略图造成障碍
如何用PHP中的Imagick模块来完美实现GIF动画缩略图
<?php
$image = new Imagick('old.gif');
$image = $image->coalesceImages();
foreach ($image as $frame) {
$frame->thumbnailImage(50, 50);
}
$image = $image->optimizeImageLayers();
$image->writeImages('new.gif', true);
?>
最关键的是coalesceimages方法 确保各帧尺寸一致 手册说是
Composites a set of images while respecting any page offsets and disposal methods. GIF, MIFF, and MNG animation sequences typically start with an image background and each subsequent image varies in size and offset. Returns a new Imagick object where each image in the sequence is the same size as the first and composited with the next image in the sequence.
同时要注意optimizeImageLayers方法删除重复像素内容 手册说
Compares each image the GIF disposed forms of the previous image in the sequence. From this it attempts to select the smallest cropped image to replace each frame, while preserving the results of the animation.
如果要求更完美一点 用 quantizeImages 方法进一步压缩
不管是 coalesceimages 还是 optimizeImageLayers 都是返回新的Imagick对象
shell操作可以这样实现GIF动画缩略图
shell> convert old.gif -coalesce -thumbnail 50x50 -layers optimize new.gif
有个细节问题 convert版本会比php版本小一些 这是API实现不一致所致
另外 如果缩略图尺寸不符合原图比例 为了避免变形 还要考虑裁剪或是补白
尊贵的董事大人
英文标题不为空时 视为本栏投稿
需要关键字 描述 英文标题