This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimGif.php
439 lines (351 loc) · 12.7 KB
/
AnimGif.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/* CHANGES of the "lunakid fork":
See CHANGES.txt for TODO items and release notes!
DONE:
+ See the GitHub commit log for the most recent (& most detailed) change history!
--------
Old (pre-1.2) manual change log (partly parallel with the GitHub commit log):
+ Renamed encodeAsciiToChar() to word2bin() & fixed its description.
+ Added error + check: 'ERR04' => 'Loading from URLs is disabled by PHP.'.
+ file_exists() -> @is_readable() (Better to take no risk of any PHP output
in a raw GIF HTTP response...)
+ Oops, also need to fix the default delay. And then also change it to 100ms.
(Because browsers seem to have difficulties handling too fast animations.)
+ Anim delay doesn't seem to be set in ms, at all. :-o
-> Yeah, they ARE NOT in millisecs! See: http://www.w3.org/Graphics/GIF/spec-gif89a.txt
Fixing the docs.
1.1:
+ Fixed ERR01 "Source is not a GIF image.": there's a .png in the examples!
-> And it does support non-GIF files actually!
Moved the error check to resource inputs only, and changed it to
"Resource is not a GIF image.".
+ create() should iterate $frames with foreach() not with for, assuming direct
indexes from 0 to < count.
(The array keys can be anything, and should not affect the results.)
+ Removed unused $mode from reporting ERR02.
+ $duration now has a meaningful default in create().
+ $frames was incorrectly made an optional arg. of create().
+ Support uniform timing without an array.
+ Separate ERR00 from $durations not being an array.
+ Fixed leftover $GIF_tim in create().
+ Renamed method getGif() to get().
+ Renamed class to AnimGif (from GifCreator).
+ Made $this->version a define (VERSION).
+ Made $this->errors really static (self::$errors).
+ Moved encodeAsciiToChar() out from the class to the namespace root as a general utility fn.
+ Moved getGif to the "public" section (and removed the Getter/Setter section).
+ Moved reset() closer up to the ctor.
+ Changed comments here & there.
+ Whitespaces: fixed some tab/space mismatch etc.
+ Changed {$i} indexes to [$i] in gifBlockCompare(), just for fun. ;)
*/
/**
* Create an animated GIF from multiple images
*
* @link https://github.com/lunakid/AnimGif
* @author Sybio (Clément Guillemain / @Sybio01), lunakid (@GitHub, @Gmail, @SO etc.)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @copyright Clément Guillemain, Szabolcs Szász
*/
namespace GifCreator;
const VERSION = '1.3+';
class AnimGif
{
const DEFAULT_DURATION = 10;
/**
* @var string: The generated (binary) image
*/
private $gif;
/**
* @var boolean: Has an image (frame) been added already?
*/
private $imgBuilt;
/**
* @var array or string: Frame sources like filenames, URLs, bin. data, or a folder name
*/
private $frameSources;
/**
* @var integer: Gif loop count
*/
private $loop;
/**
* @var integer: Gif frame disposal method
*/
private $dis;
/**
* @var integer: Gif transparent color index
*/
private $transparent_color;
/**
* @var array
*/
private static $errors;
// Methods
// ===================================================================================
public function __construct()
{
$this->reset();
self::$errors = array(
'ERR00' => 'Need at least 2 frames for an animation.',
'ERR01' => 'Resource is not a GIF image.',
'ERR02' => 'Only image resource variables, file paths, URLs or binary bitmap data are accepted.',
'ERR03' => 'Cannot make animation from animated GIF.',
'ERR04' => 'Loading from URLs is disabled by PHP.',
'ERR05' => 'Failed to load or invalid image (dir): "%s".',
);
}
/**
* Create animated GIF from source images
*
* @param array $frames The source iamges: can be a local dir path, or an array
* of file paths, resource image variables, binary data or image URLs.
* @param array|number $durations The duration (in 1/100s) of the individual frames,
* or a single integer for each one.
* @param integer $loop Number of loops before stopping the animation (set to 0 for infinite looping).
*
* @return string The resulting GIF binary data.
*/
public function create($frames, $durations = self::DEFAULT_DURATION, $loop = 0)
{
$last_duration = self::DEFAULT_DURATION; // used only if $durations is an array
$this->loop = ($loop > -1) ? $loop : 0; // Negatives would be fine for the GIF itself (all unsigned!),
// but better not take a chance with the PHP bit-ops...
$this->dis = 2; // "reset to bgnd." (http://www.matthewflickinger.com/lab/whatsinagif/animation_and_transparency.asp)
// Check if $frames is a dir; get all files in ascending order if yes (else die):
if (!is_array($frames)) {
$frames_dir = $frames;
if (@is_dir($frames_dir)) {
if ($frames = scandir($frames_dir)) {
$frames = array_filter($frames, function($x) {
// Should these two below be selectable?
return $x[0] != "."; // Or: $x != "." && $x != "..";
});
array_walk($frames, function(&$x, $i) use ($frames_dir) {
$x = "$frames_dir/$x"; });
}
}
if (!is_array($frames)) { // i.e. scandir() failed above!
throw new \Exception(VERSION.': '
. sprintf(self::$errors['ERR05'], $frames_dir)); // $frames is expected to be a string here; see the other ERR05 case!
}
}
assert(is_array($frames));
if (sizeof($frames) < 2) {
throw new \Exception(VERSION.': '.self::$errors['ERR00']);
}
$i = 0;
foreach ($frames as $frame) {
if (is_resource($frame)) { // in-memory image resource (hopefully)
$resourceImg = $frame;
ob_start();
imagegif($frame);
$this->frameSources[] = ob_get_contents();
ob_end_clean();
if (substr($this->frameSources[$i], 0, 6) != 'GIF87a' && substr($this->frameSources[$i], 0, 6) != 'GIF89a') {
throw new \Exception(VERSION.': '.$i.' '.self::$errors['ERR01']);
}
} elseif (is_string($frame)) { // file path, URL or binary data
if (@is_readable($frame)) { // file path
$bin = file_get_contents($frame);
} else if (filter_var($frame, FILTER_VALIDATE_URL)) {
if (ini_get('allow_url_fopen')) {
$bin = @file_get_contents($frame);
} else {
throw new \Exception(VERSION.': '.$i.' '.self::$errors['ERR04']);
}
} else {
$bin = $frame;
}
if (! ($bin && ($resourceImg = imagecreatefromstring($bin))) )
{
throw new \Exception(VERSION.': '.$i.' '
. sprintf(self::$errors['ERR05'], substr($frame, 0, 200))); //!! $frame may be binary data, not a name!
}
ob_start();
imagegif($resourceImg);
$this->frameSources[] = ob_get_contents();
ob_end_clean();
} else { // Fail
throw new \Exception(VERSION.': '.self::$errors['ERR02']);
}
if ($i == 0) {
$this->transparent_color = imagecolortransparent($resourceImg);
}
for ($j = (13 + 3 * (2 << (ord($this->frameSources[$i] [ 10 ]) & 0x07))), $k = TRUE; $k; $j++) {
switch ($this->frameSources[$i] [ $j ]) {
case '!':
if ((substr($this->frameSources[$i], ($j + 3), 8)) == 'NETSCAPE') {
throw new \Exception(VERSION.': '.self::$errors['ERR03'].' ('.($i + 1).' source).');
}
break;
case ';':
$k = false;
break;
}
}
unset($resourceImg);
++$i;
}//foreach
$this->gifAddHeader();
for ($i = 0; $i < count($this->frameSources); $i++) {
// Reuse the last delay if none has been specified for the current frame.
if (is_array($durations)) {
$d = (empty($durations[$i]) ? $last_duration : $durations[$i]);
$last_duration = $d;
} else {
$d = $durations;
}
$this->addGifFrames($i, $d);
}
$this->gifAddFooter();
return $this;
}
/**
* Get the resulting GIF image binary
*
* @return string
*/
public function get()
{
return $this->gif;
}
/**
* Save the resulting GIF to a file.
*
* @param $filename String Target file path
*
* @return that of file_put_contents($filename)
*/
public function save($filename)
{
return file_put_contents($filename, $this->gif);
}
/**
* Clean-up the current object (also used by the ctor.)
*/
public function reset()
{
$this->frameSources = null;
$this->gif = 'GIF89a'; // the GIF header
$this->imgBuilt = false;
$this->loop = 0;
$this->dis = 2;
$this->transparent_color = -1;
}
// Internals
// ===================================================================================
/**
* Assemble the GIF header
*/
protected function gifAddHeader()
{
$cmap = 0;
if (ord($this->frameSources[0] [ 10 ]) & 0x80) {
$cmap = 3 * (2 << (ord($this->frameSources[0] [ 10 ]) & 0x07));
$this->gif .= substr($this->frameSources[0], 6, 7);
$this->gif .= substr($this->frameSources[0], 13, $cmap);
if ($this->loop !== 1) // Only add the looping extension if really looping
$this->gif .= "!\xFF\x0BNETSCAPE2.0\x03\x01".word2bin($this->loop==0?0:$this->loop-1)."\x0";
}
}
/**
* Add frame to the GIF data
*
* @param integer $i: index of frame source
* @param integer $d: delay time (frame display duration)
*/
protected function addGifFrames($i, $d)
{
$Locals_str = 13 + 3 * (2 << (ord($this->frameSources[ $i ] [ 10 ]) & 0x07));
$Locals_end = strlen($this->frameSources[$i]) - $Locals_str - 1;
$Locals_tmp = substr($this->frameSources[$i], $Locals_str, $Locals_end);
$Global_len = 2 << (ord($this->frameSources[0 ] [ 10 ]) & 0x07);
$Locals_len = 2 << (ord($this->frameSources[$i] [ 10 ]) & 0x07);
$Global_rgb = substr($this->frameSources[ 0], 13, 3 * (2 << (ord($this->frameSources[ 0] [ 10 ]) & 0x07)));
$Locals_rgb = substr($this->frameSources[$i], 13, 3 * (2 << (ord($this->frameSources[$i] [ 10 ]) & 0x07)));
$Locals_ext = "!\xF9\x04" . chr(($this->dis << 2) + 0) . word2bin($d) . "\x0\x0";
if ($this->transparent_color > -1 && ord($this->frameSources[$i] [ 10 ]) & 0x80) {
for ($j = 0; $j < (2 << (ord($this->frameSources[$i] [ 10 ] ) & 0x07)); $j++) {
if (ord($Locals_rgb [ 3 * $j + 0 ]) == (($this->transparent_color >> 16) & 0xFF) &&
ord($Locals_rgb [ 3 * $j + 1 ]) == (($this->transparent_color >> 8) & 0xFF) &&
ord($Locals_rgb [ 3 * $j + 2 ]) == (($this->transparent_color >> 0) & 0xFF)
) {
$Locals_ext = "!\xF9\x04".chr(($this->dis << 2) + 1).chr(($d >> 0) & 0xFF).chr(($d >> 8) & 0xFF).chr($j)."\x0";
break;
}
}
}
switch ($Locals_tmp [ 0 ]) {
case '!':
$Locals_img = substr($Locals_tmp, 8, 10);
$Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);
break;
case ',':
$Locals_img = substr($Locals_tmp, 0, 10);
$Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);
break;
}
if (ord($this->frameSources[$i] [ 10 ]) & 0x80 && $this->imgBuilt) {
if ($Global_len == $Locals_len) {
if ($this->gifBlockCompare($Global_rgb, $Locals_rgb, $Global_len)) {
$this->gif .= $Locals_ext.$Locals_img.$Locals_tmp;
} else {
$byte = ord($Locals_img [9]);
$byte |= 0x80;
$byte &= 0xF8;
$byte |= (ord($this->frameSources[0] [ 10 ]) & 0x07);
$Locals_img [9] = chr($byte);
$this->gif .= $Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp;
}
} else {
$byte = ord($Locals_img [9]);
$byte |= 0x80;
$byte &= 0xF8;
$byte |= (ord($this->frameSources[$i] [ 10 ]) & 0x07);
$Locals_img [9] = chr($byte);
$this->gif .= $Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp;
}
} else {
$this->gif .= $Locals_ext.$Locals_img.$Locals_tmp;
}
$this->imgBuilt = true;
}
/**
* Add the gif string footer char
*/
protected function gifAddFooter()
{
$this->gif .= ';';
}
/**
* Compare two blocks and return 1 if they are equal, 0 if differ.
*
* @param string $globalBlock
* @param string $localBlock
* @param integer $length
*
* @return integer
*/
protected function gifBlockCompare($globalBlock, $localBlock, $length)
{
for ($i = 0; $i < $length; $i++) {
if ($globalBlock [ 3 * $i + 0 ] != $localBlock [ 3 * $i + 0 ] ||
$globalBlock [ 3 * $i + 1 ] != $localBlock [ 3 * $i + 1 ] ||
$globalBlock [ 3 * $i + 2 ] != $localBlock [ 3 * $i + 2 ]) {
return 0;
}
}
return 1;
}
}
/**
* Convert an integer to 2-byte little-endian binary data
*
* @param integer $word Number to encode
*
* @return string of 2 bytes representing @word as binary data
*/
function word2bin($word)
{
return (chr($word & 0xFF).chr(($word >> 8) & 0xFF));
}