PHP Classes

File: example/moki.php

Recommend this page to a friend!
  Classes of Patrick Van Bergen  >  Move Me GIF  >  example/moki.php  >  Download  
File: example/moki.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Move Me GIF
Create animated GIF images in pure PHP
Author: By
Last change: Clipping for accelerated frames
Set default to local color table
Use imagegif to create compressed pixel data if possible, which is much faster.
Date: 2 years ago
Size: 1,712 bytes


Class file image Download
 * This example shows a background on frame 1, followed by images of a running dog (Moki) with a transparent background on the other frames.
 * Note! This technique has an important shortcoming: when looping, the background will be shown by its own, each new loop.
 * It is not possible to start looping _after_ the background has been drawn.
 * This means that if the dog was running at a fixed position, you would see a flash of background every 10th frame.
 * The image of the running dog was taken from

use movemegif\domain\FileImageCanvas;

// just for debugging
ini_set('display_errors', 1);

// include movemegif's namespace
require_once __DIR__ . '/../php/autoloader.php';

$builder = new GifBuilder(320, 180);

// background image
setCanvas(new FileImageCanvas(__DIR__ . '/moki/landscape.jpg'));

// dogs runs from left to the right
$imageIndex = 0;
for (
$x = -140; $x <= 310; $x += 10) {

// load single frame from GIF file, and autodetect transparency color
->setCanvas(new FileImageCanvas(__DIR__ . '/moki/' . $imageIndex . '.gif'))
// number of 1/100 seconds per frame
// position this frame on the bottom half of the image
// when done painting one frame of the dog, restore the state to just before the dog was drawn

// next dog image
if (++$imageIndex == 10) {
$imageIndex = 0;