【问题标题】:Create animated gif using the GD library使用 GD 库创建动画 gif
【发布时间】:2014-10-22 15:51:23
【问题描述】:

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

我不能使用 ImageMagick 或 FFMPEG,如果可能的话,我宁愿只使用 GD 库。

显然是"Support for creating GIF animations is also available.",但我似乎找不到关于如何从 PHP 中访问它的文档?

【问题讨论】:

标签: php gd animated-gif


【解决方案1】:

这不能用 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 multiupload。

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

【讨论】:

  • 如果问题已经被问过,将其标记为重复;不要发布重复的答案。
【解决方案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

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

我自己没有尝试过,但也许它可以帮助你。

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

祝你好运

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 您可以使用 PHP exec() 函数 - 这算不算? :) 或者您可以使用 ImageMagick PHP 函数,但我从未使用过。 YMMV。
    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 2012-06-25
    • 2017-12-25
    • 2012-06-17
    • 1970-01-01
    • 2015-01-30
    • 2012-01-03
    • 2014-03-02
    相关资源
    最近更新 更多