導語:PHP使用php_imagick_st-Q8.dll類庫,把JPG圖片連線生成GIF動畫圖片,需要事先下載好php_imagick_st-Q8.dll,檔案,並配置php.ini檔案,啟用php_imagick_st-Q8.dll。下面的是本站小編蒐集的PHP使用JPG生成GIF動畫圖片例項。希望對大家能有所幫助。
配置方法如下:
1、將下載的php_imagick_st-Q8.dll檔案放到PHP預設的擴充套件目錄,也就是:php/ext/目錄內;
2、開啟php.ini,在extension區域新加入此行,注意前面不要有“;”
extension=php_imagick_st-Q8.dll
3、重啟apache或IIS。
4、PHP函式如下:
01
02//定義JPG的圖片序列
03$filelist = array(
04 '1.jpg',
05 '2.jpg',
06 '3.jpg',
07 '4.jpg'
08);
09$type = 'gif';
10$num = 200;
11$qian = 'new_';
12$path = './gif/';
13$is = 1;
14//生成gif圖片的函式
15get_img($filelist, $type, $num, $qian, $path, $is);
16/*
17 * get_img 圖片合併,生成gif動態
18 * $filelist 要合併的圖片陣列
19 * $type 生成的型別
20 * $num 生成的幀數
21 * $qian 新檔名字首
22 * $path 保持路徑
23 * $is 是否預覽
24 */
25function get_img($filelist, $type, $num, $qian, $path, $is)
26{
27 //初始化類
28 $animation = new Imagick();
29 //設定生成的格式
30 $animation->setFormat($type);
31 foreach ( $filelist as $file ){
32 $image = new Imagick();
33 $image->readImage( $file ); //合併圖片
34 $animation->addImage( $image ); //加入到物件
35 $animation->setImageDelay($num); //設定圖片幀數
36 unset( $image ); //清除記憶體裡的.影象,釋放記憶體
37 }
38 //以下兩行是除錯時用的,測試是否生成了gif圖片
39 //header( "Content-Type: image/gif" );
40 //echo( $animation->getImagesBlob() );
41 //生成的GIF檔名組合
42 $images = $qian . time(). '.' . $type;
43 //生成GIF圖片
44 $animation->writeImages( $images,true );
45 //儲存GIF到指定資料夾
46 copy($images, $path . $images);
47 //是否預覽
48 if($is)
49 {
50 echo '已生成gif圖片: ' . $images . '
';
51 echo "
";
52 }
53 else
54 {
55 echo '已生成gif圖片: ' . $images . '
';
56 }
57 //刪除原來儲存的圖片
58 unlink($images);
59}
60?>