Discuz教程网

PHP中的Imagick模块实现GIF动画缩略图

[复制链接]
authicon dly 发表于 2012-1-21 23:30:37 | 显示全部楼层 |阅读模式
缩略图是个很常用的功能。它的实现并不复杂,但如果原图是GIF动画的话,问题就会变得繁琐一点,下面通过一个取自CS警匪游戏的GIF动画来说明问题:
forum.gif   

  old.gif

  为了让问题更加清晰,我们先还原动画各帧: php程序员站   选择一:用PHP中的Imagick模块:

  1. <?php
  2. $image = new Imagick('old.gif');
  3. $i = 0;
  4. foreach ($image as $frame) {
  5. $frame->writeImage('old_' . $i++ . '.gif');  
  6. }
  7. ?>
复制代码


选择二:用ImageMagick提供的convert命令:


shell> convert old.gif old_%d.gif

  结果得到GIF动画各帧示意图如下所示:
360截图20120121233955750.jpg
  GIF动画各帧示意图

  可以明显的看到,GIF动画为了压缩,会以第一帧为模板,其余各帧按照适当的偏移量依次累加,并只保留不同的像素,结果是导致各帧尺寸不尽相同,为缩略图造成障碍。
  下面看看如何用PHP中的Imagick模块来完美实现GIF动画缩略图:

  1. <?php
  2. $image = new Imagick('old.gif');
  3. $image = $image->coalesceImages();
  4. foreach ($image as $frame) {
  5. $frame->thumbnailImage(50, 50);  
  6. }
  7. $image = $image->optimizeImageLayers();
  8. $image->writeImages('new.gif', true);
  9. ?>
复制代码


代码里最关键的是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
  有个细节问题:convert版本会比php版本小一些,这是API实现不一致所致。
  另外,如果缩略图尺寸不符合原图比例,为了避免变形,还要考虑裁剪或者是补白,由于本文主要讨论GIF动画缩略图的特殊性,就不再继续讨论这些问题了,有兴趣的自己搞定吧。



上一篇:bbs.580mxd.com申请免费空间
下一篇:完全解决Serv-U自动停止服务的问题
authicon  楼主| dly 发表于 2012-1-21 23:35:29 | 显示全部楼层

Windows 下ImageMagick的安装测试

ImageMagick (TM) 是一个免费的创建、编辑、合成图片的软件。它可以读取、转换、写入多种格式的图片。图片切割、颜色替换、各种效果的应用,图片的旋转、组合,文本,直线,多边形,椭圆,曲线,附加到图片伸展旋转。ImageMagick是免费软件:全部源码开放,可以自由使用,复制,修改,发布。它遵守GPL许可协议。它可以运行于大多数的操作系统。ImageMagick的大多数功能的使用都来源于命令行工具。通常来说,它可以支持以下程序语言: Perl, C, C++, Python, PHP, Ruby, Java;现成的ImageMagick接口(PerlMagick, Magick++, PythonMagick, MagickWand for PHP, RubyMagick, and JMagick)是可利用的。这使得自动的动态的修改创建图片变为可能。ImageMagick支持至少90种图片格式。
在PHP中使用ImageMagick需要安装扩展php_imagick.dll,下载地址:http://valokuva.org/outside-blog-content/imagick-windows-builds/ 本例使用php_imagick_st-Q16.dll。将该DLL放到php5.2的ext目录下,打开php.ini,在Windows Extensions后添加extension=php_imagick_st-Q16.dll重启Apache服务器,你可以在phpinfo()中看到:
49654f61be014aeb8cb10d57.jpg
表明imagick模块安装成功。但此时还不能正常工作,还需安装ImageMagick。
去官方下载ImageMagick-6.5.9-9-Q16-windows-dll.exe,http://www.imagemagick.org/script/binary-releases.php#windows 或者http://www.imagemagick.com.cn/download.html 和普通软件一样没什么难的,一路"next"。
测试ImageMagick,下载http://pecl.php.net/get/imagick-3.0.0b2.tgz,解压出里的examples文件夹,放到Apache文档目录下,测试例子,结果如下:
41db9f551617e42b3b2935e7.jpg
65bb65c7dd15da2f9c163de0.jpg
fdae423c066b4dda3c6d97e2.jpg
1c04262b88a1040c5243c1e3.jpg

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 18:45

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表