Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Create simple animated GIF from two JPEG images?

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

Any advice appreciated.

Thanks.

like image 824
Dan Avatar asked Feb 03 '10 11:02

Dan


People also ask

Can you animate with PHP?

Yes, it's possible.

How do you make a GIF with multiple pictures?

To do this, select File > Scripts > Load Files into Stack. A pop up window will appear that allows you to choose the GIF folder that you created in Step One. Click Browse to select and open your images and then click OK. Photoshop will create a new file with all of your images layered on top of each other.


2 Answers

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

For an even more recent solution, here is my fork of 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.)

Example that works with PHP 5.6 and 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;
like image 154
Sz. Avatar answered Sep 19 '22 12:09

Sz.


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

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

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

  • ImageMagick v6 Animation basics (from the IM manual)

  • Creating an Animated GIF image

like image 25
Pekka Avatar answered Sep 21 '22 12:09

Pekka