2011-09-07 78 views
6

如果我有一大堆使用php中的GD庫或其他一些框架製作的圖像資源,我怎樣才能將它們組合成一個動畫gif?我有一個圖像陣列和每秒幀數,我想補充..使用GD庫創建動畫gif

我不能使用ImageMagick或FFMPEG,我寧願只使用GD庫,如果可能的話。

顯然"Support for creating GIF animations is also available.",但我似乎無法找到關於如何從PHP內部訪問此文檔?

+1

可能重複[PHP - 從兩個JPEG圖像創建簡單的動畫GIF?](http://stackoverflow.com/questions/2191367/php-create-simple-animated-gif-from-two-jpeg-images) – Mat

回答

2

在Google上搜索得到的結果顯示GIFEncoder.class.php在http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html
此鏈接需要註冊。

所以我搜索了一點,它包含在code.google.com上phpvideotoolkit,可以從以下網址下載:
http://phpvideotoolkit.googlecode.com/svn-history/r6/trunk/phpvideotoolkit/adapters/ffmpeg-php/gifencoder/GIFEncoder.class.php

也有bugfixed版本只需更改文件名GIFEncoder.class。 phpvideotoolkit.php在上面的鏈接。

我還沒有嘗試過,但也許它可以幫助你。

在code.google.com上的php文件的父目錄

也是一個例子

運氣

0

與PHP捆綁在一起的AFAIK gd lib不支持創建動畫GIF。不過你可以使用gifsicle

+0

任何方式沒有命令行訪問? – Cyclone

+0

你可以使用PHP的exec()函數 - 這是否算數? :)或者你可以使用ImageMagick PHP函數,但我從來沒有使用過。因人而異。 – johndodo

4

的最好這不能與GD做,但我發現了一個偉大的圖書館吧。這有點複雜,所以這裏是一個鏈接到圖書館使用php製作GIF動畫。它解釋瞭如何徹底使用它。 http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html

你可以看一下我的例子中使用這個庫:

只需選擇兩張照片,併爲寬度和高度速度900寫100。它會將它們放入動畫GIF幻燈片中。

下面是該腳本代碼:

<?php 
if(isset($_POST['speed'])) 
{ 
    header('Content-type: image/gif'); 
    if(isset($_POST['download'])){ 
    header('Content-Disposition: attachment; filename="animated.gif"'); 
    } 
    include('GIFEncoder.class.php'); 
    function frame($image){ 
     ob_start(); 
     imagegif($image); 
     global $frames, $framed; 
     $frames[]=ob_get_contents(); 
     $framed[]=$_POST['speed']; 
     ob_end_clean(); 
    } 
    foreach ($_FILES["images"]["error"] as $key => $error) 
    { 
     if ($error == UPLOAD_ERR_OK) 
     { 
      $tmp_name = $_FILES["images"]["tmp_name"][$key]; 
      $im = imagecreatefromstring(file_get_contents($tmp_name)); 
      $resized = imagecreatetruecolor($_POST['width'],$_POST['height']); 
      imagecopyresized($resized, $im, 0, 0, 0, 0, $_POST['width'], $_POST['height'], imagesx($im), imagesy($im)); 
      frame($resized); 
     } 
    } 
    $gif = new GIFEncoder($frames,$framed,0,2,0,0,0,'bin'); 
    echo $gif->GetAnimation(); 
} 
?> 
<form action="" method="post" enctype="multipart/form-data"> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script src="jquery.MultiFile.js"></script> 
<script src="jquery.placeholder.js"></script> 
<input type="file" name="images[]" class="multi" /> 
<script> 
    $(function(){ 
     $('input[placeholder], textarea[placeholder]').placeholder(); 
    }); 
</script> 
    <SCRIPT language=Javascript> 
     <!-- 
     function isNumberKey(evt) 
     { 
     var charCode = (evt.which) ? evt.which : event.keyCode 
     if (charCode > 31 && (charCode < 48 || charCode > 57)) 
      return false; 

     return true; 
     } 
     //--> 
    </SCRIPT> 
<input name="speed" maxlength="10" type="text" placeholder="Speed of frames in ms" onkeypress="return isNumberKey(event)"> 
<input name="width" maxlength="4" type="text" placeholder="Width" onkeypress="return isNumberKey(event)"> 
<input name="height" maxlength="4" type="text" placeholder="Height" onkeypress="return isNumberKey(event)"> 
<input type="submit" name="download" value="Download!"> 
<input type="submit" name="preview" value="Preview!"> 
</form> 

正如你看到它引用的第一個鏈接上發現的GIFEncoder類。它也使用一些JavaScript驗證和jQuery multiupload。

順便說一句,這個問題已經被問到。

+5

如果問題已被問到,請將其標記爲重複;不要發佈重複的答案。 – Mat