How to Create an Animated Picture PHP

Written by declan maher
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Create an Animated Picture PHP
A series of static images that are animated can give the effect of running. (running image by Byron Moore from

An animation is created in PHP using a series of static images, producing the effect of action or movement. Animated pictures are used to improve the appearance of a website. An animated image attracts a user's attention to certain items on a Web page, such as an advertisement. In PHP you can create an animated picture using a PHP class file.

Skill level:

Things you need

  • Image-editing software

Show MoreHide


  1. 1

    Download the GifEncoder class file (see Resources).

  2. 2

    Save the class file to the webroot of the PHP website where you are creating the animated image.

  3. 3

    Create a series of PNG images that will be used to build the animated image using image-editing software. Name the images MyFirstImage.png and MySecondImage.png.

  4. 4

    Save the images to the website root.

  5. 5

    Open a PHP editor, such as Notepad.

  6. 6

    Enter PHP code to set a header for the images, and call the GIFEncoder class file

    header ('Content-type:image/gif');


  7. 7

    Open the first image for the animated image and add the following text:

    $text = "This is text I can edit on image";

    $image = imagecreatefrompng('MyFirstImage.png');

    $text_color = imagecolorallocate($image, 200, 200, 200);

    imagestring($image, 5, 5, 5, $text, $text_color);

  8. 8

    Generate a GIF from the $image:






    Add the GIF data to an array so that it can be used later by using the output buffer.

  9. 9

    Repeat the previous process to add the second image:

    $image = imagecreatefrompng('MySecondImage.png');

    $text_color = imagecolorallocate($image, 200, 200, 200);

    imagestring($image, 5, 20, 20, $text, $text_color);






    Continue adding images, as above, if you want the animation to have more than two images.

  10. 10

    Use the GIFEncode class to merge the GIF files and to generate the animated GIF:

    $gif = new GIFEncoder($frames,$framed,0,2,0,0,0,'bin');

  11. 11

    Output the completed animation:

    echo $gif->GetAnimation();

  12. 12

    Save the PHP page and open it in a browser to see the animation effect.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.