PHP使用JPG生成GIF動畫圖片,基於php_imagick_st-Q8.dll
更多php文章請閱讀
PHP使用php_imagick_st-Q8.dll類庫,把JPG圖片連接生成GIF動畫圖片,需要事先下載好php_imagick_st-Q8.dll,文件,並配置php.ini文件,啟用php_imagick_st-Q8.dll。配置方法如下:
1、將下載的php_imagick_st-Q8.dll文件放到PHP默認的擴展目錄,也就是:php/ext/目錄內;
2、打開php.ini,在extension區域新加入此行,注意前麵不要有“;”
3、重啟apache或IIS。
4、PHP函數如下:
//定義JPG的圖片序列
$filelist = array(
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg'
);
$type = 'gif';
$num = 200;
$qian = 'new_';
$path = './gif/';
$is = 1;
//生成gif圖片的函數
get_img($filelist, $type, $num, $qian, $path, $is);
/*
* get_img 圖片合並,生成gif動態
* $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 ); //清除內存裏的圖像,釋放內存
}
//以下兩行是調試時用的,測試是否生成了gif圖片
//header( "Content-Type: image/gif" );
//echo( $animation->getImagesBlob() );
//生成的GIF文件名組合
$images = $qian . time(). '.' . $type;
//生成GIF圖片
$animation->writeImages( $images,true );
//保存GIF到指定文件夾
copy($images, $path . $images);
//是否預覽
if($is)
{
echo '已生成gif圖片: ' . $images . '<br />';
echo "<img src='" . $path . $images . "' />";
}
else
{
echo '已生成gif圖片: ' . $images . '<br />';
}
//刪除原來保存的圖片
unlink($images);
}
?>
最後更新:2017-08-18 16:02:27