Skip to content

Commit 256b646

Browse files
committed
Add examples: Manage animated GIF with ImageWorkshop
1 parent be4094f commit 256b646

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This directory contains source code for the [Manage animated GIF with ImageWorkshop](../../doc/tutorials/manage-animated-gif.md) tutorial.
2+
3+
An [online version](http://phpimageworkshop.com/tutorial/5/manage-animated-gif-with-imageworkshop.html) also exists.
4+
5+
To use it, you need to install [Composer](https://getcomposer.org) dependency manager.
6+
7+
```bash
8+
composer install
9+
10+
php run.php
11+
```
12+
13+
It will create `output.gif` file in the current directory.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "image-workshop/example-animated-gif",
3+
"description": "Source code for \"Manage animated GIF with ImageWorkshop\"",
4+
"type": "project",
5+
"license": "MIT",
6+
"require": {
7+
"sybio/gif-creator": "^1.0",
8+
"sybio/gif-frame-extractor": "^1.0",
9+
"sybio/image-workshop": "^2.0"
10+
}
11+
}
Loading
516 Bytes
Loading

examples/manage-animated-gif/run.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
require __DIR__.'/vendor/autoload.php';
4+
5+
use GifCreator\GifCreator;
6+
use GifFrameExtractor\GifFrameExtractor;
7+
use PHPImageWorkshop\ImageWorkshop;
8+
9+
$gifPath = __DIR__.'/images/original.gif'; // Your animated GIF path
10+
if (GifFrameExtractor::isAnimatedGif($gifPath)) { // check this is an animated GIF
11+
12+
// Extractions of the GIF frames and their durations
13+
$gfe = new GifFrameExtractor();
14+
$frames = $gfe->extract($gifPath);
15+
16+
// Initialization of the watermark layer
17+
$watermarkLayer = ImageWorkshop::initFromPath(__DIR__.'/images/watermark.png');
18+
19+
// For each frame, we add a watermark and we resize it
20+
$retouchedFrames = array();
21+
foreach ($frames as $frame) {
22+
23+
// Initialization of the frame as a layer
24+
$frameLayer = ImageWorkshop::initFromResourceVar($frame['image']);
25+
26+
$frameLayer->resizeInPixel(350, null, true); // Resizing
27+
$frameLayer->addLayerOnTop($watermarkLayer, 20, 20, 'LB'); // Watermarking
28+
29+
$retouchedFrames[] = $frameLayer->getResult();
30+
}
31+
32+
// Then we re-generate the GIF
33+
$gc = new GifCreator();
34+
$gc->create($retouchedFrames, $gfe->getFrameDurations(), 0);
35+
36+
// And now save it !
37+
file_put_contents(__DIR__.'/output.gif', $gc->getGif());
38+
}

0 commit comments

Comments
 (0)