Make an animated GIF with PHP's ImageMagick API

11,895

While I'm not a PHP expert, I know that this issue isn't a too difficult one. What you want to do is create an Imagick object that you can append your frames to. With each frame you can change parameters like timing etc.

Assuming you're working with images that are uploaded from a basic web form, I've written a basic example that loops through images that were uploaded with a name of "image0", where "0" goes up to however many files are included. You could naturally just add images by using the same methods on fixed file names or whatever.

$GIF = new Imagick();
$GIF->setFormat("gif");

for ($i = 0; $i < sizeof($_FILES); ++$i) {
    $frame = new Imagick();
    $frame->readImage($_FILES["image$i"]["tmp_name"]);
    $frame->setImageDelay(10);
    $GIF->addImage($frame);
}

header("Content-Type: image/gif");
echo $GIF->getImagesBlob();

This example creates an Imagick object that is what will become our GIF. The files that were uploaded to the server are then looped through and each one is firstly read (remember however that this technique relies on that the images are named as I described above), secondly it gets a delay value, and thirdly, it's appended to the GIF-to-be. That's the basic idea, and it will produce what you're after (I hope).

But there's lot to tamper with, and your configuration may look different. I always found the php.net Imagick API reference to kind of suck, but it's still nice to have and I use it every now and then to reference things from the standard ImageMagick.

Hope this somewhat matches what you were after.

Share:
11,895
joedborg
Author by

joedborg

Updated on June 04, 2022

Comments

  • joedborg
    joedborg about 2 years

    I can do it easily in my OS

    convert -delay 1/1 -loop 0 *.gif animated.gif
    

    But I can't find how to do this in the PHP API. No resizing or anything needed, I've just got a set of frames that need animating.

  • joedborg
    joedborg about 12 years
    +1 for the well written answer but it doesn't seem to work. I'm making an array of files and then lopping over each with the code you've done (and the last example here php.net/manual/en/imagick.addimage.php) but all I see is the last image. Even if I set to 500ms and wait. I'd like to loop and can't find that either.
  • joedborg
    joedborg about 12 years
    Ah, this work if you use writeImages rather then echo. I'd rather echo though. Any ideas?
  • fredrikekelund
    fredrikekelund about 12 years
    My bad, I forgot an important detail there, editing the answer :) The getImagesBlob() method corresponds to setting the second argument in writeImages() to true. And this will echo a the animated GIF as a blob, which should work.
  • joedborg
    joedborg about 12 years
    Just worth mentioning here that writeImages() doesn't appear to work in Imagick 3.
  • Ryan H
    Ryan H about 3 years
    Sent some time on this and I was having the same issue of only seeing the last image when I used morphImages() .... solution was that after loading all the images I needed to call $GIF->resetIterator(); to get back to the first image in the list ... hope this helps save someone a lot of time. Fond it from this post geeksforgeeks.org/php-imagick-morphimages-function