完美实现GIF动画缩略图的php代码


Posted in PHP onJanuary 02, 2011

下面通过一个取自CS警匪游戏的GIF动画来说明问题:

完美实现GIF动画缩略图的php代码

GIF动画图片:old.gif

为了让问题更加清晰,我们先还原动画各帧:

选择一:用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动画各帧示意图如下所示:

完美实现GIF动画缩略图的php代码

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.

BTW:如果要求更完美一点,可以使用quantizeImages方法进一步压缩。

注意:不管是coalesceimages,还是optimizeImageLayers,都是返回新的Imagick对象!

如果你更习惯操作shell的话,那么可以这样实现GIF动画缩略图:

shell> convert old.gif -coalesce -thumbnail 50x50 -layers optimize new.gif

生成的new.gif如下:

 

完美实现GIF动画缩略图的php代码

new.gif

有个细节问题:convert版本会比php版本小一些,这是API实现不一致所致。

另外,如果缩略图尺寸不符合原图比例,为了避免变形,还要考虑裁剪或者是补白,由于本文主要讨论GIF动画缩略图的特殊性,就不再继续讨论这些问题了,有兴趣的自己搞定吧。

PHP 相关文章推荐
Banner程序
Oct 09 PHP
一个比较简单的PHP 分页分组类
Dec 10 PHP
PHP通过iconv将字符串从GBK转换为UTF8字符集
Jul 18 PHP
php实现自动获取生成文章主题关键词功能的深入分析
Jun 03 PHP
解析smarty模板中类似for的功能实现
Jun 18 PHP
不使用php api函数实现数组的交换排序示例
Apr 13 PHP
php采用ajax数据提交post与post常见方法总结
Nov 10 PHP
php中array_slice和array_splice函数解析
Oct 18 PHP
php实现留言板功能
Mar 05 PHP
php版本CKEditor 4和CKFinder安装及配置方法图文教程
Jun 05 PHP
php文件包含的几种方式总结
Sep 19 PHP
解决thinkphp5未定义变量会抛出异常,页面错误,请稍后再试的问题
Oct 16 PHP
php实现无限级分类实现代码(递归方法)
Jan 01 #PHP
php下尝试使用GraphicsMagick的缩略图功能
Jan 01 #PHP
PHP读取XML值的代码(推荐)
Jan 01 #PHP
PHP中simplexml_load_string函数使用说明
Jan 01 #PHP
php xml 入门学习资料
Jan 01 #PHP
PHP+SQL 注入攻击的技术实现以及预防办法
Dec 29 #PHP
解决PHP在DOS命令行下却无法链接MySQL的技术笔记
Dec 29 #PHP
You might like
xml在joomla表单中的应用详解分享
2012/07/19 PHP
php函数传值的引用传递注意事项分析
2016/06/25 PHP
JavaScript 继承详解(四)
2009/07/13 Javascript
jQuery中bind与live的用法及区别小结
2014/01/27 Javascript
Javascript中判断对象是否为空
2015/06/10 Javascript
jQuery实现拖拽页面元素并将其保存到cookie的方法
2016/06/12 Javascript
AngularJS封装指令方法详解
2016/12/12 Javascript
基于AngularJS的简单使用详解
2017/09/10 Javascript
vue使用xe-utils函数库的具体方法
2018/03/06 Javascript
AngularJS中的作用域实例分析
2018/05/16 Javascript
JS中‘hello’与new String(‘hello’)引出的问题详解
2018/08/14 Javascript
详解如何探测小程序返回到webview页面
2019/05/14 Javascript
Vue.directive 实现元素scroll逻辑复用
2019/11/29 Javascript
js实现拾色器插件(ColorPicker)
2020/05/21 Javascript
[04:11]DOTA2亚洲邀请赛小组赛第一日 TOP10精彩集锦
2015/01/30 DOTA
Pyramid Mako模板引入helper对象的步骤方法
2013/11/27 Python
python通过floor函数舍弃小数位的方法
2015/03/17 Python
在Python中封装GObject模块进行图形化程序编程的教程
2015/04/14 Python
深入剖析Python的爬虫框架Scrapy的结构与运作流程
2016/01/20 Python
python 开发的三种运行模式详细介绍
2017/01/18 Python
使用Python实现博客上进行自动翻页
2017/08/23 Python
python生成器,可迭代对象,迭代器区别和联系
2018/02/04 Python
python3实现域名查询和whois查询功能
2018/06/21 Python
基于python cut和qcut的用法及区别详解
2019/11/22 Python
详解CSS3媒体查询响应式布局bootstrap 框架原理实战(推荐)
2020/11/16 HTML / CSS
三好生演讲稿
2014/09/12 职场文书
没有孩子的离婚协议书怎么写
2014/09/17 职场文书
党员先进性教育整改措施
2014/09/18 职场文书
交通违章检讨书
2014/09/21 职场文书
2014年教育教学工作总结
2014/11/13 职场文书
男方婚前保证书
2015/02/28 职场文书
2015年党风廉政建设工作总结
2015/04/09 职场文书
欠条范文
2015/07/03 职场文书
运动会闭幕式致辞
2015/07/29 职场文书
七年级作文之游记
2019/12/11 职场文书
python数据处理之Pandas类型转换
2022/04/28 Python