Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ErikvdVen/php-gif
Easy generating (real-time) GIF images with PHP
https://github.com/ErikvdVen/php-gif
Last synced: 5 days ago
JSON representation
Easy generating (real-time) GIF images with PHP
- Host: GitHub
- URL: https://github.com/ErikvdVen/php-gif
- Owner: ErikvdVen
- Created: 2015-12-17T23:25:48.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-01-15T17:10:06.000Z (10 months ago)
- Last Synced: 2024-09-20T11:07:35.903Z (about 2 months ago)
- Language: PHP
- Homepage: http://only-media.nl/gif/gif.php
- Size: 601 KB
- Stars: 900
- Watchers: 31
- Forks: 62
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG-1.0.md
Awesome Lists containing this project
README
# php-gif
[![Latest Stable Version](https://poser.pugx.org/erikvdven/php-gif/v/stable)](https://packagist.org/packages/erikvdven/php-gif)
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=erikvandeven100%40hotmail%2ecom&lc=NL&item_name=Erik%20van%20de%20Ven%20Github)**IMPORTANT NOTE:** This repository is not actively maintained anymore. I moved from PHP to Python years ago, so that's basically why. If you would like to contribute on this repository, feel free to open up a PR and perhaps I can add you as a contributor as well. There are also people who forked this repository and decided to built further on that. For a fork which is compatible with PHP 8, see [phpgif/phpgif](https://github.com/phpgif/phpgif)
Below GIF image is an example 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. 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.
For below image this doesn't work, unfortunately, because GitHub downloads the image once and stores it locally. Most e-mail clients, however, do load the images directly from source. Successfully tested with Gmail and Outlook 2011 (Mac OSX).
## 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](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
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.
```php
5,
'frames' => array(
array(
'image' => './images/newyear.jpg',
'text' => array(
array(
'text' => 'Hello GIF frame 1',
'fonts' => './fonts/Lato-Light.ttf',
'fonts-size' => 30,
'angle' => 0,
'fonts-color' => '#000',
'x-position' => 140,
'y-position' => 138
)
),
'delay' => 100
),
array(
'image' => './images/newyear.jpg',
'text' => array(
array(
'text' => 'Hello GIF frame 2',
'fonts' => './fonts/Lato-Light.ttf',
'fonts-size' => 15,
'angle' => 0,
'fonts-color' => '#000',
'x-position' => 140,
'y-position' => 138
),
array(
'text' => 'Hello GIF frame 2',
'fonts' => './fonts/Lato-Light.ttf',
'fonts-size' => 15,
'angle' => 0,
'fonts-color' => '#000',
'x-position' => 140,
'y-position' => 108
)
),
'delay' => 100
),
array(
'image' => './images/newyear.jpg',
'delay' => 50
)
)
);echo $gif->generate($imageFrames);
?>
```## License & Credits
This software is published under the [MIT License](https://en.wikipedia.org/wiki/MIT_License).
###### GIFEncoder
GIFEncoder.class.php contains minor adaptations from the GIFEncoder PHP class by [László Zsidi](http://gifs.hu).