Php 使用GD库创建动画gif

Php 使用GD库创建动画gif,php,gd,animated-gif,Php,Gd,Animated Gif,如果我有一堆用php中的GD库制作的图像资源,或者一堆帧,如何将它们组合成动画gif?我有一组图像和每秒帧数,我想添加 我不能使用ImageMagick或FFMPEG,如果可能的话,我更愿意使用GD库 显然,但是我似乎找不到关于如何从PHP中访问它的文档?与PHP捆绑的AFAIK gd库不支持创建动画GIF。不过您可以使用。在Google Encoder.class.php上搜索,可以在 此链接需要注册 所以我搜索了一下,它包含在code.google.com上的phpvideoolkit中,可

如果我有一堆用php中的GD库制作的图像资源,或者一堆帧,如何将它们组合成动画gif?我有一组图像和每秒帧数,我想添加

我不能使用ImageMagick或FFMPEG,如果可能的话,我更愿意使用GD库


显然,但是我似乎找不到关于如何从PHP中访问它的文档?

与PHP捆绑的AFAIK gd库不支持创建动画GIF。不过您可以使用。

在Google Encoder.class.php上搜索,可以在
此链接需要注册

所以我搜索了一下,它包含在code.google.com上的phpvideoolkit中,可以在以下网址下载:

还有一个错误修复版本,只需在上面的链接中将文件名更改为GIFEncoder.class.phpvideotoolkit.php

我自己没试过,但也许它能帮你

在code.google.com上php文件的父目录中也是一个例子


祝你好运

这不能用GD完成,但我找到了一个很棒的库。虽然有点复杂,但这里有一个指向库的链接,该库使用php制作动画GIF。它解释了如何彻底地使用它

您可以使用以下库查看我的示例:

只需选择2张图片,并将100写入speed 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>

$(函数(){
$('input[placeholder],textarea[placeholder]).placeholder();
});
正如您所看到的,它引用了在第一个链接上找到的GIFEncoder类。它还使用了一些javascript验证和jquerymultiupload


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

您可以使用PHP exec()函数-这算吗?:)或者可以使用ImageMagick PHP函数,但我从未使用过。YMMV.可能的副本如果问题已经提出,则将其标记为副本;不要发布重复的答案。