Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

php-gif

> 编程语言
Open source

Easy generating (real-time) GIF images with PHP

896 stars0 likes2 views
WebsiteGitHub

About

Easy generating (real-time) GIF images with PHP

php-gif

Project status

Over the years my main focus has shifted from PHP to Python and Java, and as a result PHP is no longer part of my day-to-day work. Because of that I'm not actively developing new features for this project. That said, I'm still happy to review and merge open Pull Requests. If you're waiting for feedback, feel free to send me a message on LinkedIn (see my profile page).

Example

examples/countdown/countdown.php contains an example of generating an image which can contain real-time data. A PHP script calculates the pending time till new year and generates the GIF image. An ideal solution for sending e-mails with real-time data to customers. Some E-mail clients give you the opportunity to load images by URL and so everytime the client re-opens the e-mail message, the GIF will be re-generated with real-time data.

Note

I received some questions about Gmail so lets describe this mail client a bit more in detail. This script is successfully tested with Gmail, at the moment of writing, which is 2015-12-23. Gmail loads the images via their own proxy, so not directly from source. There are different opinions about the proxy, but it seems that Google's proxy protects your private data and only informs the sender that the email has been opened. There are speculations that Gmail caches the images, but still respect the cache headers, so you can instruct Gmail how often to refresh the data. I personally had no trouble with the cache whatsoever! I've tested this countdown image numerous times in Gmail and of course you can test it yourself as well. Just to be sure I've added some cache disabling headers in the examples.

Composer Installation

  1. Get Composer
  2. Require php-gif with composer require erikvdven/php-gif
  3. Add the following to your application's main PHP file: require 'vendor/autoload.php';

Getting Started

Create a PHP file and add these headers at the beginning of the file:

php
// Caching disable headers
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

// Output as a GIF image
header ('Content-type:image/gif');

// Include the GIFGenerator class
use ErikvdVen\Gif\GIFGenerator;

On the next lines you can create a GIF image by first initializing the GIFGenerator object and creating an array with all the image frames:

php
// Initialize a new GIFGenerator object
$gif = new GIFGenerator();

// Create a multidimensional array with all the image frames
$imageFrames = array(
	'repeat' => false,
	'frames' => array(
		array(
			'image' => './images/newyear.jpg',
			'text' => array(
				array(
					'text' => 'Hello GIF frame 1',
					'fonts-color' => '#000',
					'x-position' => 140,
					'y-position' => 138
				)
			),
			'delay' => 100
		),
	)
);

Finally you generate the image and echo the results on the screen:

php
echo $gif->generate($imageFrames);

Example

A more complete example. You could copy/paste below code to a file and execute it in the browser to view a more complete result. As you can see it's not required to use text in your GIF image and you can add as much text per frame, and as much frames per GIF image as you like.

…

License & Credits

This software is published under the MIT License.

GIFEncoder

GIFEncoder.class.php contains minor adaptations from the GIFEncoder PHP class by László Zsidi.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言