PHPackages                             erikvdven/php-gif - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. erikvdven/php-gif

ActiveLibrary

erikvdven/php-gif
=================

1.1.0(2mo ago)89841.0k621MITPHPPHP &gt;=5.4.0

Since Jan 21Pushed 2mo ago28 watchersCompare

[ Source](https://github.com/ErikvdVen/php-gif)[ Packagist](https://packagist.org/packages/erikvdven/php-gif)[ Docs](https://github.com/ErikvdVen/php-gif)[ RSS](/packages/erikvdven-php-gif/feed)WikiDiscussions main Synced 2mo ago

READMEChangelog (1)DependenciesVersions (4)Used By (1)

php-gif
=======

[](#php-gif)

[![Latest Stable Version](https://camo.githubusercontent.com/3959dd8d3c9b9845aec437bd9e21d1d9e4ed5c234d7a09fc9ea5f548ab7647b9/68747470733a2f2f706f7365722e707567782e6f72672f6572696b766476656e2f7068702d6769662f762f737461626c65)](https://packagist.org/packages/erikvdven/php-gif)

Project status
--------------

[](#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
-------

[](#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
----

[](#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
---------------------

[](#composer-installation)

1. [Get Composer](https://getcomposer.org/)
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
---------------

[](#getting-started)

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

```
// 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:

```
// 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:

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

Example
-------

[](#example-1)

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.

```
