php_imagick_st-Q8.dll 放入 PHP ext/ 目錄php.ini 添加:extension=php_imagick_st-Q8.dll(去掉分號)<?php
$filelist = array('1.jpg', '2.jpg', '3.jpg', '4.jpg');
$type = 'gif';
$num = 200; // 幀延遲(毫秒)
$qian = 'new_'; // 輸出文件前綴
$path = './gif/'; // 保存路徑
$is = 1; // 是否預覽
get_img($filelist, $type, $num, $qian, $path, $is);
function get_img($filelist, $type, $num, $qian, $path, $is) {
$animation = new Imagick();
$animation->setFormat($type);
foreach ($filelist as $file) {
$image = new Imagick();
$image->readImage($file);
$animation->addImage($image);
$animation->setImageDelay($num);
unset($image); // 釋放內存
}
$images = $qian . time() . '.' . $type;
$animation->writeImages($images, true); // true = 多幀合併
copy($images, $path . $images);
unlink($images); // 刪除臨時文件
if ($is) {
echo "已生成: $images <br/><img src='$path$images' />";
} else {
echo "已生成: $images <br/>";
}
}
setImageDelay($num):設置幀延遲,單位為百分之一秒(200 = 2 秒)writeImages($filename, true):第二參數 true 表示將所有幀寫入單個文件unset($image):及時釋放單幀內存,防止大量圖片導致內存溢出Imagick 還支援圖片縮放、裁剪、特效等高級操作,可結合
resizeImage()、cropImage()實現更豐富的動畫生成需求。