使用GD库创建动态GIF

6
如果我有一堆使用php中GD库制作的图像资源,或者有很多帧,我应该如何将它们合并为一个动态gif?我有一组图像和每秒的帧数要添加…
如果可能的话,我不想使用ImageMagick或FFMPEG,而是更喜欢只使用GD库。
显然“也可以创建GIF动画。”,但我似乎无法找到如何从PHP中访问此功能的文档?

3个回答

5
这不能通过GD库实现,但我发现了一款很好的库。这个库有点复杂,所以在这里提供一个链接,它使用PHP生成动态GIF图像。链接中详细解释了如何使用它。http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html 你可以查看我使用这个库的示例:
只需选择2张图片,将速度设置为100,宽度和高度设置为900。它会把它们放入一个动态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多上传。

顺便说一句,这个问题已经被问过了。


6
如果问题已经被问过了,请将其标记为重复;不要发布重复的答案。 - Mat

2
在谷歌上搜索发现了GIFEncoder.class.php,可以在http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html找到它,但需要注册。
我进一步搜寻后发现它包含在phpvideotoolkit中,可以通过以下链接下载:http://phpvideotoolkit.googlecode.com/svn-history/r6/trunk/phpvideotoolkit/adapters/ffmpeg-php/gifencoder/GIFEncoder.class.php。此外,还有一个修正错误的版本,只需将文件名改为GIFEncoder.class.phpvideotoolkit.php即可。
我没有亲自尝试过,但也许它能帮助您。在code.google.com的php文件父目录中也有一个示例。
祝你好运!

0
据我所知,与PHP捆绑在一起的gd库不支持创建动画GIF。但是你可以使用gifsicle

你可以使用PHP的exec()函数 - 那算吗? :) 或者你可以使用ImageMagick PHP函数,但我从未使用过。可能会因人而异。 - johndodo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接