PHP - 从两个 JPEG 图像创建简单的动画 GIF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2191367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:26:11  来源:igfitidea点击:

PHP - Create simple animated GIF from two JPEG images?

phpimagegifanimated-gif

提问by Dan

Does anyone know if it's possible to generate an animated GIF from two different JPEG files, displaying one image for x seconds then the other, and so on..?

有谁知道是否可以从两个不同的 JPEG 文件生成动画 GIF,显示一个图像 x 秒然后另一个,依此类推..?

Any advice appreciated.

任何建议表示赞赏。

Thanks.

谢谢。

采纳答案by Pekka

It's not possible using the standard GD functions that come pre-packed with PHP.

使用 PHP 预装的标准 GD 函数是不可能的。

There is a class on phpclasses.orgfor this. I have never used it myself, but it's used by a lot of other packages.

phpclasses.org 上有一个课程。我自己从未使用过它,但很多其他软件包都使用过它。

Alternatively, if you have access to ImageMagickfrom PHP, using either the MagickWandlibrary or the command line, use it. With ImageMagick, it's no problem.

或者,如果您可以使用MagickWand库或命令行从 PHP访问ImageMagick,请使用它。使用 ImageMagick,没问题。

回答by Sz.

For a nice, quick and more recent solution, see this SO answer.

有关一个不错的、快速的和更新的解决方案,请参阅此 SO answer

For an even more recent solution, here is my forkof it, with a number of small fixes & improvements. An example of it from an actual application:

对于更新的解决方案,这里是我的分支,有一些小的修复和改进。一个来自实际应用的例子:

$anim = new GifCreator\AnimGif();

$gif = $anim->create($image_files);
//file_put_contents("test.gif", $gif);

header("Content-type: image/gif");
echo $gif;

(Requires PHP5.3 with GD2.)

(需要 PHP5.3 和 GD2。)

Example that works with PHP 5.6 and GD 2.4.11:

适用于 PHP 5.6 和 GD 2.4.11 的示例:

require_once "AnimGif.php";

/*
 * Create an array containing file paths, resource var (initialized with imagecreatefromXXX), 
 * image URLs or even binary code from image files.
 * All sorted in order to appear.
 */
$image_files = array(
    //imagecreatefrompng("/../images/pic1.png"), // Resource var
    //"/../images/pic2.png", // Image file path
    //file_get_contents("/../images/pic3.jpg"), // Binary source code
    'https://yt3.ggpht.com/-KxeE9Hu93eE/AAAAAAAAAAI/AAAAAAAAAAA/D-DB1Umuimk/s100-c-k-no-mo-rj-c0xffffff/photo.jpg', // URL
    'https://media.licdn.com/mpr/mpr/shrinknp_100_100/AAEAAQAAAAAAAAloAAAAJDRkZGY2MWZmLTM1NDYtNDBhOS04MjYwLWNkM2UzYjdiZGZmMA.png', // URL
    'http://is5.mzstatic.com/image/thumb/Purple128/v4/e4/63/e7/e463e779-e6d0-0c3d-3ec1-97fdbaae230a/source/100x100bb.jpg' // URL
);

/*
 * Create an array containing the duration (in millisecond) of each frame.
 */
$durations_millis = array(
    1000,
    2000,
    3000
);

/*
 * Fix durations.
 */
$durations = array();
for ($i = 0; $i < count($durations_millis); $i++) {
    $durations[$i] = $durations_millis[$i] / 10;
}

/*
 * Specify number of loops. (0 = infinite looping.)
 */
$num_loops = 0;

/*
 * Create gif object.
 */
$anim_gif = new GifCreator\AnimGif();
$gif_object = $anim_gif->create($image_files, $durations, $num_loops);

/*
 * Get the animated GIF binary.
 */
$gif_binary = $gif_object->get();

/*
 * Set the file name of the saved/returned animated GIF.
 */
$file_name = "animated.gif";

/*
 *  Optionally, save animated GIF in a folder as a GIF:
 */
//file_put_contents($file_name, $gif_binary);

/*
 * Optionally, return the animated GIF to client.
 */
header("Content-type: image/gif");
header('Content-Disposition: filename="' . $file_name . '"'); // Optional
echo $gif_binary;

/*
 * All done.
 */
exit;

回答by Tom

This cannot be done with GD but I found a great library for it. It is a bit complicated though, so here is a link to the library which makes animated gifs with php. It explains how to use it thoroughly. http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html

这不能用 GD 完成,但我找到了一个很棒的库。不过它有点复杂,所以这里有一个链接到使用 php 制作动画 gif 的库。它解释了如何彻底使用它。http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html

Select 2 pictures and write 100 for speed 900 for width and height. It will put them in an animated gif slideshow.

选择2张图片,速度写100,宽高写900。它将把它们放在动画 gif 幻灯片中。

Here is the code for that script:

这是该脚本的代码:

<?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>

As you see it references the GIFEncoder class found on the first link. It also uses some javascript validation and jQuery multiupload.

如您所见,它引用了第一个链接上的 GIFEncoder 类。它还使用了一些 javascript 验证和 jQuery multiupload。

Note: this question has already been asked.

注意:这个问题已经被问过了。