【问题标题】:PHP - Create simple animated GIF from two JPEG images? [closed]PHP - 从两个 JPEG 图像创建简单的动画 GIF?
【发布时间】:2011-01-12 13:53:00
【问题描述】:

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

任何建议表示赞赏。

谢谢。

【问题讨论】:

    标签: php image gif animated-gif


    【解决方案1】:

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

    为此有一个class on phpclasses.org。我自己没用过,但是很多其他包都用过。

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

    【讨论】:

    • phpclasses.org 代码是访问的噩梦,它的输入文件必须是 GIF 文件。它不适用于 JPEG 文件... :-/
    • 您可以使用另一个 SO 问题的答案将输入的 JPEG 图像转换为(静态)GIF 图像:stackoverflow.com/a/755843/1617737
    【解决方案2】:

    如需一个不错、快速且更新的解决方案,请参阅this SO answer

    对于更新的解决方案,here is my fork 其中包含一些小的修复和改进。来自实际应用的示例:

    $anim = new GifCreator\AnimGif();
    
    $gif = $anim->create($image_files);
    //file_put_contents("test.gif", $gif);
    
    header("Content-type: image/gif");
    echo $gif;
    

    (需要 PHP5.3 和 GD2。)

    适用于 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;
    

    【讨论】:

    • 这不适用于我的(PHP5.6 和 GD2.4.11)。给我The image "..." cannot be displayed because it contains errors.
    • @ban-geoengineering,感谢您指出这一点。 (嗯,一切皆有可能,因为当时还没有 PHP5.6。)一个可能的修复刚刚被合并到那里的代码中;请重试,如果问题仍然存在,请在 GitHub 上向项目提交问题,以便在那里妥善解决。
    • 谢谢。刚刚使用工作代码 sn-p 更新了您的答案。
    【解决方案3】:

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

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

    【讨论】:

    • @Tom 当我尝试此代码时返回错误,例如“无法显示,因为它包含错误。”你能帮帮我吗?
    猜你喜欢
    • 2011-04-10
    • 2019-10-20
    • 2020-07-25
    • 2017-06-13
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2016-11-11
    相关资源
    最近更新 更多