色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

完美實現GIF動畫縮略圖的php代碼

下面通過一個取自CS警匪游戲的GIF動畫來說明問題: GIF動畫圖片:old.gif

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動畫各幀示意圖

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如下:

 

new.gif

new.gif

有個細節問題:convert版本會比php版本小一些,這是API實現不一致所致。

另外,如果縮略圖尺寸不符合原圖比例,為了避免變形,還要考慮裁剪或者是補白,由于本文主要討論GIF動畫縮略圖的特殊性,就不再繼續討論這些問題了,有興趣的自己搞定吧。

php技術完美實現GIF動畫縮略圖的php代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 龙陵县| 万盛区| 和顺县| 九龙县| 昌江| 札达县| 大兴区| 皮山县| 天水市| 承德市| 镇巴县| 开封县| 比如县| 南陵县| 教育| 兴宁市| 临夏市| 潮安县| 大厂| 东阳市| 庆元县| 松原市| 星子县| 高安市| 准格尔旗| 鄢陵县| 绵阳市| 大方县| 横峰县| 周口市| 庄浪县| 五莲县| 南郑县| 讷河市| 观塘区| 鄂伦春自治旗| 德惠市| 东源县| 驻马店市| 天柱县| 四会市|