完美实现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 相关文章推荐
使用GROUP BY的时候如何统计记录条数 COUNT(*) DISTINCT
Apr 23 PHP
php中常用的预定义变量小结
May 09 PHP
php旋转图片90度的方法
Nov 07 PHP
使用PHP导出Redis数据到另一个Redis中的代码
Mar 12 PHP
php使用smtp发送支持附件的邮件示例
Apr 13 PHP
PHP判断IP并转跳到相应城市分站的方法
Mar 25 PHP
PHP中的switch语句的用法实例详解
Oct 21 PHP
php实现的xml操作类
Jan 15 PHP
php 中的closure用法详解
Jun 12 PHP
深入浅析PHP的session反序列化漏洞问题
Jun 15 PHP
php将从数据库中获得的数据转换成json格式并输出的方法
Aug 21 PHP
Laravel推荐使用的十个辅助函数
May 10 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
smarty 缓存控制前的页面静态化原理
2013/03/15 PHP
Php无限级栏目分类读取的实现代码
2014/02/19 PHP
ThinkPHP上使用多说评论插件的方法
2014/10/31 PHP
浅谈PHP中Stream(流)
2015/06/08 PHP
zen_cart实现支付前生成订单的方法
2016/05/06 PHP
php下的原生ajax请求用法实例分析
2020/02/28 PHP
Nigma vs Alliance BO5 第三场2.14
2021/03/10 DOTA
js模拟类继承小例子
2010/07/17 Javascript
jQuery Validation实例代码 让验证变得如此容易
2010/10/18 Javascript
JavaScript全局函数使用简单说明
2011/03/11 Javascript
JavaScript实现打字效果的方法
2015/07/10 Javascript
使用AmplifyJS组件配合JavaScript进行编程的指南
2015/07/28 Javascript
jQuery+ajax实现文章点赞功能的方法
2015/12/31 Javascript
javascript的列表切换【实现代码】
2016/05/03 Javascript
jQuery弹出下拉列表插件(实现kindeditor的@功能)
2016/08/16 Javascript
js遍历json对象所有key及根据动态key获取值的方法(必看)
2017/03/09 Javascript
jQuery实现IE输入框完成placeholder标签功能的方法
2017/09/20 jQuery
Node做中转服务器转发接口
2017/10/18 Javascript
Vue 自定义动态组件实例详解
2018/03/28 Javascript
express+vue+mongodb+session 实现注册登录功能
2018/12/06 Javascript
[02:42]决战东方!DOTA2亚洲邀请赛重启荣耀之争
2017/03/17 DOTA
[01:55]TI9显影之尘系列 - Evil Geniuses
2019/08/22 DOTA
python 多进程通信模块的简单实现
2014/02/20 Python
python爬虫入门教程--HTML文本的解析库BeautifulSoup(四)
2017/05/25 Python
python实现对文件中图片生成带标签的txt文件方法
2018/04/27 Python
django利用request id便于定位及给日志加上request_id
2018/08/26 Python
微软香港官网及网上商店:Microsoft HK
2016/09/01 全球购物
Servlet都有哪些方法?主要作用是什么?
2014/03/04 面试题
餐饮业创业计划书范文
2014/01/06 职场文书
中学自我评价
2014/01/31 职场文书
后勤部经理岗位职责
2014/02/23 职场文书
logback如何自定义日志存储
2021/08/30 Java/Android
VS2019连接MySQL数据库的过程及常见问题总结
2021/11/27 MySQL
SQL Server远程连接的设置步骤(图文)
2022/03/23 SQL Server
Python获取字典中某个key的value
2022/04/13 Python
苹果macOS 13开发者预览版Beta 8发布 正式版10月发布
2022/09/23 数码科技