Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create animated gif using the GD library

If I've got a bunch of image resources that I made using the GD library in php, or otherwise a bunch of frames, how can I combine these into an animated gif? I've got an array of images and a frames per second I'd like to add..

I can't use ImageMagick or FFMPEG, and I'd prefer to just use the GD library if possible.

Apparently "Support for creating GIF animations is also available.", but I can't seem to find the documentation on how to access this from within PHP?

like image 443
Cyclone Avatar asked Sep 07 '11 20:09

Cyclone


People also ask

How do you make your own animated GIFs?

Visit giphy.com/create/gifmaker to get started. Choose Photo or GIF to add by dragging and dropping, clicking the blue search button to browse your files, or entering image URLs to add files to your slideshow. Drag and drop images to change the order of your images to change the way they will appear in your slideshow.


2 Answers

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

You can look at my example using this library:

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

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.

BTW this question was already asked.

like image 131
Tom Avatar answered Oct 05 '22 06:10

Tom


Well searching on Google revealed GIFEncoder.class.php found at http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html
This link requires registration.

So i searched a little and it is included in phpvideotoolkit on code.google.com and can be downloaded at:
http://phpvideotoolkit.googlecode.com/svn-history/r6/trunk/phpvideotoolkit/adapters/ffmpeg-php/gifencoder/GIFEncoder.class.php

there is also a bugfixed version just change the file name to GIFEncoder.class.phpvideotoolkit.php in the above link.

I haven't tried it myself but maybe it can help you.

in the parent directory of the php file on code.google.com is also an example

best of luck

like image 35
danishgoel Avatar answered Oct 05 '22 06:10

danishgoel