Viewed   140 times

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.

 Answers

4

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

Friday, September 30, 2022
 
pisamce
 
5

http://www.php.net/manual/en/book.imagick.php

See animated GIF example here: http://valokuva.org/?p=46

Sunday, November 6, 2022
 
2

See this solution.

https://github.com/nbadal/android-gif-encoder

It's an Android version of this post.

http://www.jappit.com/blog/2008/12/04/j2me-animated-gif-encoder/

To use this class, here is an example helper method to generate GIF byte array. Note here the getBitmapArray() function is a method to return all the Bitmap files in an image adapter at once. So the input is all the Bitmap files in one adapter, the output is a byte array which you can write to the file.

public byte[] generateGIF() {
    ArrayList<Bitmap> bitmaps = adapter.getBitmapArray();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    AnimatedGifEncoder encoder = new AnimatedGifEncoder();
    encoder.start(bos);
    for (Bitmap bitmap : bitmaps) {
        encoder.addFrame(bitmap);
    }
    encoder.finish();
    return bos.toByteArray();
}

To use this function, do the following then you can save the file into SDcard.

FileOutputStream outStream = null;
        try{
            outStream = new FileOutputStream("/sdcard/generate_gif/test.gif");
            outStream.write(generateGIF());
            outStream.close();
        }catch(Exception e){
            e.printStackTrace();
        }
Thursday, August 4, 2022
 
dale_z
 
5

You can do this with ffmpeg

First convert the images to a video:

ffmpeg -f image2 -i image%d.jpg video.avi

(This will convert the images from the current directory (named image1.jpg, image2.jpg...) to a video file named video.avi.)

Then convert the avi to a gif:

ffmpeg -i video.avi -pix_fmt rgb24 -loop_output 0 out.gif

You can get windows binaries for ffmpeg here.


You can also do a similar thing with mplayer. See Encoding from multiple input image files.

I think the command line would be something like:

mplayer mf://*.jpg -mf w=800:h=600:type=jpg -vf scale=160:120 -vo gif89a:fps=3:output=out.gif

(Where 800 & 600 are your source width and height and 160 & 120 are the target width and height.out.gif is your target file name)


I've just tested both of these and they both work fine. However I got much better results from mplayer as I was able to specify the resolution and framerate. Your milage may vary and I'm sure you could find more options for ffmpeg if you looked.

Saturday, September 3, 2022
5

Use your own code up until the call of SelectActiveFrame() and after that change to this lines:

frames[0] = new Bitmap(GG);
pictureBox1.Image = frame[0];

This should do the trick. Please do not forget do dispose the created Images.

Sunday, October 16, 2022
 
stawho
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :