|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Classes used for reading gif files (in case PHP's GD doesn't provide the |
|
5
|
|
|
* proper gif-functions). |
|
6
|
|
|
* |
|
7
|
|
|
* Gif Util copyright 2003 by Yamasoft (S/C). All rights reserved. |
|
8
|
|
|
* Do not remove this portion of the header, or use these functions except |
|
9
|
|
|
* from the original author. To get it, please navigate to: |
|
10
|
|
|
* http://www.yamasoft.com/php-gif.zip |
|
11
|
|
|
* |
|
12
|
|
|
* Simple Machines Forum (SMF) |
|
13
|
|
|
* |
|
14
|
|
|
* @package SMF |
|
15
|
|
|
* @author Simple Machines http://www.simplemachines.org |
|
16
|
|
|
* @copyright 2017 Simple Machines and individual contributors |
|
17
|
|
|
* @license http://www.simplemachines.org/about/smf/license.php BSD |
|
18
|
|
|
* |
|
19
|
|
|
* @version 2.1 Beta 4 |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
if (!defined('SMF')) |
|
23
|
|
|
die('No direct access...'); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class gif_lzw_compression |
|
27
|
|
|
* |
|
28
|
|
|
* An implementation of the LZW compression algorithm |
|
29
|
|
|
*/ |
|
30
|
|
|
class gif_lzw_compression |
|
31
|
|
|
{ |
|
32
|
|
|
public $MAX_LZW_BITS; |
|
33
|
|
|
public $Fresh, $CodeSize, $SetCodeSize, $MaxCode, $MaxCodeSize, $FirstCode, $OldCode; |
|
34
|
|
|
public $ClearCode, $EndCode, $Next, $Vals, $Stack, $sp, $Buf, $CurBit, $LastBit, $Done, $LastByte; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->MAX_LZW_BITS = 12; |
|
39
|
|
|
unset($this->Next, $this->Vals, $this->Stack, $this->Buf); |
|
40
|
|
|
|
|
41
|
|
|
$this->Next = range(0, (1 << $this->MAX_LZW_BITS) - 1); |
|
42
|
|
|
$this->Vals = range(0, (1 << $this->MAX_LZW_BITS) - 1); |
|
43
|
|
|
$this->Stack = range(0, (1 << ($this->MAX_LZW_BITS + 1)) - 1); |
|
44
|
|
|
$this->Buf = range(0, 279); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function decompress($data, &$datLen) |
|
48
|
|
|
{ |
|
49
|
|
|
$stLen = strlen($data); |
|
50
|
|
|
$datLen = 0; |
|
51
|
|
|
$ret = ''; |
|
52
|
|
|
|
|
53
|
|
|
$this->LZWCommand($data, true); |
|
54
|
|
|
|
|
55
|
|
|
while (($iIndex = $this->LZWCommand($data, false)) >= 0) |
|
56
|
|
|
$ret .= chr($iIndex); |
|
57
|
|
|
|
|
58
|
|
|
$datLen = $stLen - strlen($data); |
|
59
|
|
|
|
|
60
|
|
|
if ($iIndex != -2) |
|
61
|
|
|
return false; |
|
62
|
|
|
|
|
63
|
|
|
return $ret; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function LZWCommand(&$data, $bInit) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($bInit) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->SetCodeSize = ord($data[0]); |
|
71
|
|
|
$data = substr($data, 1); |
|
72
|
|
|
|
|
73
|
|
|
$this->CodeSize = $this->SetCodeSize + 1; |
|
74
|
|
|
$this->ClearCode = 1 << $this->SetCodeSize; |
|
75
|
|
|
$this->EndCode = $this->ClearCode + 1; |
|
76
|
|
|
$this->MaxCode = $this->ClearCode + 2; |
|
77
|
|
|
$this->MaxCodeSize = $this->ClearCode << 1; |
|
78
|
|
|
|
|
79
|
|
|
$this->GetCode($data, $bInit); |
|
80
|
|
|
|
|
81
|
|
|
$this->Fresh = 1; |
|
82
|
|
View Code Duplication |
for ($i = 0; $i < $this->ClearCode; $i++) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$this->Next[$i] = 0; |
|
85
|
|
|
$this->Vals[$i] = $i; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
View Code Duplication |
for (; $i < (1 << $this->MAX_LZW_BITS); $i++) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$this->Next[$i] = 0; |
|
91
|
|
|
$this->Vals[$i] = 0; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->sp = 0; |
|
95
|
|
|
return 1; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($this->Fresh) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->Fresh = 0; |
|
101
|
|
|
do |
|
102
|
|
|
{ |
|
103
|
|
|
$this->FirstCode = $this->GetCode($data, $bInit); |
|
104
|
|
|
$this->OldCode = $this->FirstCode; |
|
105
|
|
|
} |
|
106
|
|
|
while ($this->FirstCode == $this->ClearCode); |
|
107
|
|
|
|
|
108
|
|
|
return $this->FirstCode; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ($this->sp > 0) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->sp--; |
|
114
|
|
|
return $this->Stack[$this->sp]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
while (($Code = $this->GetCode($data, $bInit)) >= 0) |
|
118
|
|
|
{ |
|
119
|
|
|
if ($Code == $this->ClearCode) |
|
120
|
|
|
{ |
|
121
|
|
View Code Duplication |
for ($i = 0; $i < $this->ClearCode; $i++) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
$this->Next[$i] = 0; |
|
124
|
|
|
$this->Vals[$i] = $i; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
View Code Duplication |
for (; $i < (1 << $this->MAX_LZW_BITS); $i++) |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
$this->Next[$i] = 0; |
|
130
|
|
|
$this->Vals[$i] = 0; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->CodeSize = $this->SetCodeSize + 1; |
|
134
|
|
|
$this->MaxCodeSize = $this->ClearCode << 1; |
|
135
|
|
|
$this->MaxCode = $this->ClearCode + 2; |
|
136
|
|
|
$this->sp = 0; |
|
137
|
|
|
$this->FirstCode = $this->GetCode($data, $bInit); |
|
138
|
|
|
$this->OldCode = $this->FirstCode; |
|
139
|
|
|
|
|
140
|
|
|
return $this->FirstCode; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ($Code == $this->EndCode) |
|
144
|
|
|
return -2; |
|
145
|
|
|
|
|
146
|
|
|
$InCode = $Code; |
|
147
|
|
|
if ($Code >= $this->MaxCode) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->Stack[$this->sp] = $this->FirstCode; |
|
150
|
|
|
$this->sp++; |
|
151
|
|
|
$Code = $this->OldCode; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
while ($Code >= $this->ClearCode) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->Stack[$this->sp] = $this->Vals[$Code]; |
|
157
|
|
|
$this->sp++; |
|
158
|
|
|
|
|
159
|
|
|
if ($Code == $this->Next[$Code]) // Circular table entry, big GIF Error! |
|
160
|
|
|
return -1; |
|
161
|
|
|
|
|
162
|
|
|
$Code = $this->Next[$Code]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$this->FirstCode = $this->Vals[$Code]; |
|
166
|
|
|
$this->Stack[$this->sp] = $this->FirstCode; |
|
167
|
|
|
$this->sp++; |
|
168
|
|
|
|
|
169
|
|
|
if (($Code = $this->MaxCode) < (1 << $this->MAX_LZW_BITS)) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->Next[$Code] = $this->OldCode; |
|
172
|
|
|
$this->Vals[$Code] = $this->FirstCode; |
|
173
|
|
|
$this->MaxCode++; |
|
174
|
|
|
|
|
175
|
|
|
if (($this->MaxCode >= $this->MaxCodeSize) && ($this->MaxCodeSize < (1 << $this->MAX_LZW_BITS))) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->MaxCodeSize *= 2; |
|
178
|
|
|
$this->CodeSize++; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$this->OldCode = $InCode; |
|
183
|
|
|
if ($this->sp > 0) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->sp--; |
|
186
|
|
|
return $this->Stack[$this->sp]; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $Code; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function GetCode(&$data, $bInit) |
|
194
|
|
|
{ |
|
195
|
|
|
if ($bInit) |
|
196
|
|
|
{ |
|
197
|
|
|
$this->CurBit = 0; |
|
198
|
|
|
$this->LastBit = 0; |
|
199
|
|
|
$this->Done = 0; |
|
200
|
|
|
$this->LastByte = 2; |
|
201
|
|
|
|
|
202
|
|
|
return 1; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
if (($this->CurBit + $this->CodeSize) >= $this->LastBit) |
|
206
|
|
|
{ |
|
207
|
|
|
if ($this->Done) |
|
208
|
|
|
{ |
|
209
|
|
|
// Ran off the end of my bits... |
|
210
|
|
|
if ($this->CurBit >= $this->LastBit) |
|
211
|
|
|
return 0; |
|
212
|
|
|
|
|
213
|
|
|
return -1; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$this->Buf[0] = $this->Buf[$this->LastByte - 2]; |
|
217
|
|
|
$this->Buf[1] = $this->Buf[$this->LastByte - 1]; |
|
218
|
|
|
|
|
219
|
|
|
$count = ord($data[0]); |
|
220
|
|
|
$data = substr($data, 1); |
|
221
|
|
|
|
|
222
|
|
|
if ($count) |
|
223
|
|
|
{ |
|
224
|
|
|
for ($i = 0; $i < $count; $i++) |
|
225
|
|
|
$this->Buf[2 + $i] = ord($data{$i}); |
|
226
|
|
|
|
|
227
|
|
|
$data = substr($data, $count); |
|
228
|
|
|
} |
|
229
|
|
|
else |
|
230
|
|
|
$this->Done = 1; |
|
231
|
|
|
|
|
232
|
|
|
$this->LastByte = 2 + $count; |
|
233
|
|
|
$this->CurBit = ($this->CurBit - $this->LastBit) + 16; |
|
234
|
|
|
$this->LastBit = (2 + $count) << 3; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$iRet = 0; |
|
238
|
|
|
for ($i = $this->CurBit, $j = 0; $j < $this->CodeSize; $i++, $j++) |
|
239
|
|
|
$iRet |= (($this->Buf[intval($i / 8)] & (1 << ($i % 8))) != 0) << $j; |
|
240
|
|
|
|
|
241
|
|
|
$this->CurBit += $this->CodeSize; |
|
242
|
|
|
return $iRet; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
class gif_color_table |
|
247
|
|
|
{ |
|
248
|
|
|
public $m_nColors; |
|
249
|
|
|
public $m_arColors; |
|
250
|
|
|
|
|
251
|
|
|
public function __construct() |
|
252
|
|
|
{ |
|
253
|
|
|
unset($this->m_nColors, $this->m_arColors); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function load($lpData, $num) |
|
257
|
|
|
{ |
|
258
|
|
|
$this->m_nColors = 0; |
|
259
|
|
|
$this->m_arColors = array(); |
|
260
|
|
|
|
|
261
|
|
|
for ($i = 0; $i < $num; $i++) |
|
262
|
|
|
{ |
|
263
|
|
|
$rgb = substr($lpData, $i * 3, 3); |
|
264
|
|
|
if (strlen($rgb) < 3) |
|
265
|
|
|
return false; |
|
266
|
|
|
|
|
267
|
|
|
$this->m_arColors[] = (ord($rgb[2]) << 16) + (ord($rgb[1]) << 8) + ord($rgb[0]); |
|
268
|
|
|
$this->m_nColors++; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
return true; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
public function toString() |
|
275
|
|
|
{ |
|
276
|
|
|
$ret = ''; |
|
277
|
|
|
|
|
278
|
|
|
for ($i = 0; $i < $this->m_nColors; $i++) |
|
279
|
|
|
{ |
|
280
|
|
|
$ret .= |
|
281
|
|
|
chr(($this->m_arColors[$i] & 0x000000FF)) . // R |
|
282
|
|
|
chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G |
|
283
|
|
|
chr(($this->m_arColors[$i] & 0x00FF0000) >> 16); // B |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return $ret; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
public function colorIndex($rgb) |
|
290
|
|
|
{ |
|
291
|
|
|
$rgb = intval($rgb) & 0xFFFFFF; |
|
292
|
|
|
$r1 = ($rgb & 0x0000FF); |
|
293
|
|
|
$g1 = ($rgb & 0x00FF00) >> 8; |
|
294
|
|
|
$b1 = ($rgb & 0xFF0000) >> 16; |
|
295
|
|
|
$idx = -1; |
|
296
|
|
|
|
|
297
|
|
|
for ($i = 0; $i < $this->m_nColors; $i++) |
|
298
|
|
|
{ |
|
299
|
|
|
$r2 = ($this->m_arColors[$i] & 0x000000FF); |
|
300
|
|
|
$g2 = ($this->m_arColors[$i] & 0x0000FF00) >> 8; |
|
301
|
|
|
$b2 = ($this->m_arColors[$i] & 0x00FF0000) >> 16; |
|
302
|
|
|
$d = abs($r2 - $r1) + abs($g2 - $g1) + abs($b2 - $b1); |
|
303
|
|
|
|
|
304
|
|
|
if (($idx == -1) || ($d < $dif)) |
|
|
|
|
|
|
305
|
|
|
{ |
|
306
|
|
|
$idx = $i; |
|
307
|
|
|
$dif = $d; |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
return $idx; |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
class gif_file_header |
|
316
|
|
|
{ |
|
317
|
|
|
public $m_lpVer, $m_nWidth, $m_nHeight, $m_bGlobalClr, $m_nColorRes; |
|
318
|
|
|
public $m_bSorted, $m_nTableSize, $m_nBgColor, $m_nPixelRatio; |
|
319
|
|
|
public $m_colorTable; |
|
320
|
|
|
|
|
321
|
|
|
public function __construct() |
|
322
|
|
|
{ |
|
323
|
|
|
unset($this->m_lpVer, $this->m_nWidth, $this->m_nHeight, $this->m_bGlobalClr, $this->m_nColorRes); |
|
324
|
|
|
unset($this->m_bSorted, $this->m_nTableSize, $this->m_nBgColor, $this->m_nPixelRatio, $this->m_colorTable); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
public function load($lpData, &$hdrLen) |
|
328
|
|
|
{ |
|
329
|
|
|
$hdrLen = 0; |
|
330
|
|
|
|
|
331
|
|
|
$this->m_lpVer = substr($lpData, 0, 6); |
|
332
|
|
|
if (($this->m_lpVer != 'GIF87a') && ($this->m_lpVer != 'GIF89a')) |
|
333
|
|
|
return false; |
|
334
|
|
|
|
|
335
|
|
|
list ($this->m_nWidth, $this->m_nHeight) = array_values(unpack('v2', substr($lpData, 6, 4))); |
|
336
|
|
|
|
|
337
|
|
|
if (!$this->m_nWidth || !$this->m_nHeight) |
|
338
|
|
|
return false; |
|
339
|
|
|
|
|
340
|
|
|
$b = ord(substr($lpData, 10, 1)); |
|
341
|
|
|
$this->m_bGlobalClr = ($b & 0x80) ? true : false; |
|
342
|
|
|
$this->m_nColorRes = ($b & 0x70) >> 4; |
|
343
|
|
|
$this->m_bSorted = ($b & 0x08) ? true : false; |
|
344
|
|
|
$this->m_nTableSize = 2 << ($b & 0x07); |
|
345
|
|
|
$this->m_nBgColor = ord(substr($lpData, 11, 1)); |
|
346
|
|
|
$this->m_nPixelRatio = ord(substr($lpData, 12, 1)); |
|
347
|
|
|
$hdrLen = 13; |
|
348
|
|
|
|
|
349
|
|
View Code Duplication |
if ($this->m_bGlobalClr) |
|
|
|
|
|
|
350
|
|
|
{ |
|
351
|
|
|
$this->m_colorTable = new gif_color_table(); |
|
352
|
|
|
if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) |
|
353
|
|
|
return false; |
|
354
|
|
|
|
|
355
|
|
|
$hdrLen += 3 * $this->m_nTableSize; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
return true; |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
class gif_image_header |
|
363
|
|
|
{ |
|
364
|
|
|
public $m_nLeft, $m_nTop, $m_nWidth, $m_nHeight, $m_bLocalClr; |
|
365
|
|
|
public $m_bInterlace, $m_bSorted, $m_nTableSize, $m_colorTable; |
|
366
|
|
|
|
|
367
|
|
|
public function __construct() |
|
368
|
|
|
{ |
|
369
|
|
|
unset($this->m_nLeft, $this->m_nTop, $this->m_nWidth, $this->m_nHeight, $this->m_bLocalClr); |
|
370
|
|
|
unset($this->m_bInterlace, $this->m_bSorted, $this->m_nTableSize, $this->m_colorTable); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
public function load($lpData, &$hdrLen) |
|
374
|
|
|
{ |
|
375
|
|
|
$hdrLen = 0; |
|
376
|
|
|
|
|
377
|
|
|
// Get the width/height/etc. from the header. |
|
378
|
|
|
list ($this->m_nLeft, $this->m_nTop, $this->m_nWidth, $this->m_nHeight) = array_values(unpack('v4', substr($lpData, 0, 8))); |
|
379
|
|
|
|
|
380
|
|
|
if (!$this->m_nWidth || !$this->m_nHeight) |
|
381
|
|
|
return false; |
|
382
|
|
|
|
|
383
|
|
|
$b = ord($lpData[8]); |
|
384
|
|
|
$this->m_bLocalClr = ($b & 0x80) ? true : false; |
|
385
|
|
|
$this->m_bInterlace = ($b & 0x40) ? true : false; |
|
386
|
|
|
$this->m_bSorted = ($b & 0x20) ? true : false; |
|
387
|
|
|
$this->m_nTableSize = 2 << ($b & 0x07); |
|
388
|
|
|
$hdrLen = 9; |
|
389
|
|
|
|
|
390
|
|
View Code Duplication |
if ($this->m_bLocalClr) |
|
|
|
|
|
|
391
|
|
|
{ |
|
392
|
|
|
$this->m_colorTable = new gif_color_table(); |
|
393
|
|
|
if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) |
|
394
|
|
|
return false; |
|
395
|
|
|
|
|
396
|
|
|
$hdrLen += 3 * $this->m_nTableSize; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
return true; |
|
400
|
|
|
} |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
class gif_image |
|
404
|
|
|
{ |
|
405
|
|
|
public $m_disp, $m_bUser, $m_bTrans, $m_nDelay, $m_nTrans, $m_lpComm; |
|
406
|
|
|
public $m_gih, $m_data, $m_lzw; |
|
407
|
|
|
|
|
408
|
|
|
public function __construct() |
|
409
|
|
|
{ |
|
410
|
|
|
unset($this->m_disp, $this->m_bUser, $this->m_nDelay, $this->m_nTrans, $this->m_lpComm, $this->m_data); |
|
411
|
|
|
$this->m_gih = new gif_image_header(); |
|
412
|
|
|
$this->m_lzw = new gif_lzw_compression(); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
public function load($data, &$datLen) |
|
416
|
|
|
{ |
|
417
|
|
|
$datLen = 0; |
|
418
|
|
|
|
|
419
|
|
|
while (true) |
|
420
|
|
|
{ |
|
421
|
|
|
$b = ord($data[0]); |
|
422
|
|
|
$data = substr($data, 1); |
|
423
|
|
|
$datLen++; |
|
424
|
|
|
|
|
425
|
|
|
switch ($b) |
|
426
|
|
|
{ |
|
427
|
|
|
// Extension... |
|
428
|
|
|
case 0x21: |
|
429
|
|
|
$len = 0; |
|
430
|
|
|
if (!$this->skipExt($data, $len)) |
|
431
|
|
|
return false; |
|
432
|
|
|
|
|
433
|
|
|
$datLen += $len; |
|
434
|
|
|
break; |
|
435
|
|
|
|
|
436
|
|
|
// Image... |
|
437
|
|
|
case 0x2C: |
|
438
|
|
|
// Load the header and color table. |
|
439
|
|
|
$len = 0; |
|
440
|
|
|
if (!$this->m_gih->load($data, $len)) |
|
441
|
|
|
return false; |
|
442
|
|
|
|
|
443
|
|
|
$data = substr($data, $len); |
|
444
|
|
|
$datLen += $len; |
|
445
|
|
|
|
|
446
|
|
|
// Decompress the data, and ride on home ;). |
|
447
|
|
|
$len = 0; |
|
448
|
|
|
if (!($this->m_data = $this->m_lzw->decompress($data, $len))) |
|
449
|
|
|
return false; |
|
450
|
|
|
|
|
451
|
|
|
$datLen += $len; |
|
452
|
|
|
|
|
453
|
|
|
if ($this->m_gih->m_bInterlace) |
|
454
|
|
|
$this->deInterlace(); |
|
455
|
|
|
|
|
456
|
|
|
return true; |
|
457
|
|
|
|
|
458
|
|
|
case 0x3B: // EOF |
|
459
|
|
|
default: |
|
460
|
|
|
return false; |
|
461
|
|
|
} |
|
462
|
|
|
} |
|
463
|
|
|
return false; |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
public function skipExt(&$data, &$extLen) |
|
467
|
|
|
{ |
|
468
|
|
|
$extLen = 0; |
|
469
|
|
|
|
|
470
|
|
|
$b = ord($data[0]); |
|
471
|
|
|
$data = substr($data, 1); |
|
472
|
|
|
$extLen++; |
|
473
|
|
|
|
|
474
|
|
|
switch ($b) |
|
475
|
|
|
{ |
|
476
|
|
|
// Graphic Control... |
|
477
|
|
|
case 0xF9: |
|
478
|
|
|
$b = ord($data[1]); |
|
479
|
|
|
$this->m_disp = ($b & 0x1C) >> 2; |
|
480
|
|
|
$this->m_bUser = ($b & 0x02) ? true : false; |
|
481
|
|
|
$this->m_bTrans = ($b & 0x01) ? true : false; |
|
482
|
|
|
list ($this->m_nDelay) = array_values(unpack('v', substr($data, 2, 2))); |
|
483
|
|
|
$this->m_nTrans = ord($data[4]); |
|
484
|
|
|
break; |
|
485
|
|
|
|
|
486
|
|
|
// Comment... |
|
487
|
|
|
case 0xFE: |
|
488
|
|
|
$this->m_lpComm = substr($data, 1, ord($data[0])); |
|
489
|
|
|
break; |
|
490
|
|
|
|
|
491
|
|
|
// Plain text... |
|
492
|
|
|
case 0x01: |
|
493
|
|
|
break; |
|
494
|
|
|
|
|
495
|
|
|
// Application... |
|
496
|
|
|
case 0xFF: |
|
497
|
|
|
break; |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
// Skip default as defs may change. |
|
501
|
|
|
$b = ord($data[0]); |
|
502
|
|
|
$data = substr($data, 1); |
|
503
|
|
|
$extLen++; |
|
504
|
|
|
while ($b > 0) |
|
505
|
|
|
{ |
|
506
|
|
|
$data = substr($data, $b); |
|
507
|
|
|
$extLen += $b; |
|
508
|
|
|
$b = ord($data[0]); |
|
509
|
|
|
$data = substr($data, 1); |
|
510
|
|
|
$extLen++; |
|
511
|
|
|
} |
|
512
|
|
|
return true; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
public function deInterlace() |
|
516
|
|
|
{ |
|
517
|
|
|
$data = $this->m_data; |
|
518
|
|
|
|
|
519
|
|
|
for ($i = 0; $i < 4; $i++) |
|
520
|
|
|
{ |
|
521
|
|
|
switch ($i) |
|
522
|
|
|
{ |
|
523
|
|
|
case 0: |
|
524
|
|
|
$s = 8; |
|
525
|
|
|
$y = 0; |
|
526
|
|
|
break; |
|
527
|
|
|
|
|
528
|
|
|
case 1: |
|
529
|
|
|
$s = 8; |
|
530
|
|
|
$y = 4; |
|
531
|
|
|
break; |
|
532
|
|
|
|
|
533
|
|
|
case 2: |
|
534
|
|
|
$s = 4; |
|
535
|
|
|
$y = 2; |
|
536
|
|
|
break; |
|
537
|
|
|
|
|
538
|
|
|
case 3: |
|
539
|
|
|
$s = 2; |
|
540
|
|
|
$y = 1; |
|
541
|
|
|
break; |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
for (; $y < $this->m_gih->m_nHeight; $y += $s) |
|
|
|
|
|
|
545
|
|
|
{ |
|
546
|
|
|
$lne = substr($this->m_data, 0, $this->m_gih->m_nWidth); |
|
547
|
|
|
$this->m_data = substr($this->m_data, $this->m_gih->m_nWidth); |
|
548
|
|
|
|
|
549
|
|
|
$data = |
|
550
|
|
|
substr($data, 0, $y * $this->m_gih->m_nWidth) . |
|
551
|
|
|
$lne . |
|
552
|
|
|
substr($data, ($y + 1) * $this->m_gih->m_nWidth); |
|
553
|
|
|
} |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
$this->m_data = $data; |
|
557
|
|
|
} |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
class gif_file |
|
561
|
|
|
{ |
|
562
|
|
|
public $header, $image, $data, $loaded; |
|
563
|
|
|
|
|
564
|
|
|
public function __construct() |
|
565
|
|
|
{ |
|
566
|
|
|
$this->data = ''; |
|
567
|
|
|
$this->loaded = false; |
|
568
|
|
|
$this->header = new gif_file_header(); |
|
569
|
|
|
$this->image = new gif_image(); |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
public function loadFile($filename, $iIndex) |
|
573
|
|
|
{ |
|
574
|
|
|
if ($iIndex < 0) |
|
575
|
|
|
return false; |
|
576
|
|
|
|
|
577
|
|
|
$this->data = @file_get_contents($filename); |
|
578
|
|
|
if ($this->data === false) |
|
579
|
|
|
return false; |
|
580
|
|
|
|
|
581
|
|
|
// Tell the header to load up.... |
|
582
|
|
|
$len = 0; |
|
583
|
|
|
if (!$this->header->load($this->data, $len)) |
|
584
|
|
|
return false; |
|
585
|
|
|
|
|
586
|
|
|
$this->data = substr($this->data, $len); |
|
587
|
|
|
|
|
588
|
|
|
// Keep reading (at least once) so we get to the actual image we're looking for. |
|
589
|
|
|
for ($j = 0; $j <= $iIndex; $j++) |
|
590
|
|
|
{ |
|
591
|
|
|
$imgLen = 0; |
|
592
|
|
|
if (!$this->image->load($this->data, $imgLen)) |
|
593
|
|
|
return false; |
|
594
|
|
|
|
|
595
|
|
|
$this->data = substr($this->data, $imgLen); |
|
596
|
|
|
} |
|
597
|
|
|
|
|
598
|
|
|
$this->loaded = true; |
|
599
|
|
|
return true; |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
public function get_png_data($background_color) |
|
603
|
|
|
{ |
|
604
|
|
|
if (!$this->loaded) |
|
605
|
|
|
return false; |
|
606
|
|
|
|
|
607
|
|
|
// Prepare the color table. |
|
608
|
|
|
if ($this->image->m_gih->m_bLocalClr) |
|
609
|
|
|
{ |
|
610
|
|
|
$colors = $this->image->m_gih->m_nTableSize; |
|
611
|
|
|
$pal = $this->image->m_gih->m_colorTable->toString(); |
|
612
|
|
|
|
|
613
|
|
|
if ($background_color != -1) |
|
614
|
|
|
$background_color = $this->image->m_gih->m_colorTable->colorIndex($background_color); |
|
615
|
|
|
} |
|
616
|
|
|
elseif ($this->header->m_bGlobalClr) |
|
617
|
|
|
{ |
|
618
|
|
|
$colors = $this->header->m_nTableSize; |
|
619
|
|
|
$pal = $this->header->m_colorTable->toString(); |
|
620
|
|
|
|
|
621
|
|
|
if ($background_color != -1) |
|
622
|
|
|
$background_color = $this->header->m_colorTable->colorIndex($background_color); |
|
623
|
|
|
} |
|
624
|
|
|
else |
|
625
|
|
|
{ |
|
626
|
|
|
$colors = 0; |
|
627
|
|
|
$background_color = -1; |
|
628
|
|
|
} |
|
629
|
|
|
|
|
630
|
|
|
if ($background_color == -1) |
|
631
|
|
|
$background_color = $this->header->m_nBgColor; |
|
632
|
|
|
|
|
633
|
|
|
$data = &$this->image->m_data; |
|
634
|
|
|
$header = &$this->image->m_gih; |
|
635
|
|
|
|
|
636
|
|
|
$i = 0; |
|
637
|
|
|
$bmp = ''; |
|
638
|
|
|
|
|
639
|
|
|
// Prepare the bitmap itself. |
|
640
|
|
|
for ($y = 0; $y < $this->header->m_nHeight; $y++) |
|
641
|
|
|
{ |
|
642
|
|
|
$bmp .= "\x00"; |
|
643
|
|
|
|
|
644
|
|
|
for ($x = 0; $x < $this->header->m_nWidth; $x++, $i++) |
|
645
|
|
|
{ |
|
646
|
|
|
// Is this in the proper range? If so, get the specific pixel data... |
|
647
|
|
|
if ($x >= $header->m_nLeft && $y >= $header->m_nTop && $x < ($header->m_nLeft + $header->m_nWidth) && $y < ($header->m_nTop + $header->m_nHeight)) |
|
648
|
|
|
$bmp .= $data{$i}; |
|
649
|
|
|
// Otherwise, this is background... |
|
650
|
|
|
else |
|
651
|
|
|
$bmp .= chr($background_color); |
|
652
|
|
|
} |
|
653
|
|
|
} |
|
654
|
|
|
|
|
655
|
|
|
$bmp = gzcompress($bmp, 9); |
|
656
|
|
|
|
|
657
|
|
|
// Output the basic signature first of all. |
|
658
|
|
|
$out = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"; |
|
659
|
|
|
|
|
660
|
|
|
// Now, we want the header... |
|
661
|
|
|
$out .= "\x00\x00\x00\x0D"; |
|
662
|
|
|
$tmp = 'IHDR' . pack('N', (int) $this->header->m_nWidth) . pack('N', (int) $this->header->m_nHeight) . "\x08\x03\x00\x00\x00"; |
|
663
|
|
|
$out .= $tmp . pack('N', smf_crc32($tmp)); |
|
664
|
|
|
|
|
665
|
|
|
// The palette, assuming we have one to speak of... |
|
666
|
|
|
if ($colors > 0) |
|
667
|
|
|
{ |
|
668
|
|
|
$out .= pack('N', (int) $colors * 3); |
|
669
|
|
|
$tmp = 'PLTE' . $pal; |
|
|
|
|
|
|
670
|
|
|
$out .= $tmp . pack('N', smf_crc32($tmp)); |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
// Do we have any transparency we want to make available? |
|
674
|
|
|
if ($this->image->m_bTrans && $colors > 0) |
|
675
|
|
|
{ |
|
676
|
|
|
$out .= pack('N', (int) $colors); |
|
677
|
|
|
$tmp = 'tRNS'; |
|
678
|
|
|
|
|
679
|
|
|
// Stick each color on - full transparency or none. |
|
680
|
|
|
for ($i = 0; $i < $colors; $i++) |
|
681
|
|
|
$tmp .= $i == $this->image->m_nTrans ? "\x00" : "\xFF"; |
|
682
|
|
|
|
|
683
|
|
|
$out .= $tmp . pack('N', smf_crc32($tmp)); |
|
684
|
|
|
} |
|
685
|
|
|
|
|
686
|
|
|
// Here's the data itself! |
|
687
|
|
|
$out .= pack('N', strlen($bmp)); |
|
688
|
|
|
$tmp = 'IDAT' . $bmp; |
|
689
|
|
|
$out .= $tmp . pack('N', smf_crc32($tmp)); |
|
690
|
|
|
|
|
691
|
|
|
// EOF marker... |
|
692
|
|
|
$out .= "\x00\x00\x00\x00" . 'IEND' . "\xAE\x42\x60\x82"; |
|
693
|
|
|
|
|
694
|
|
|
return $out; |
|
695
|
|
|
} |
|
696
|
|
|
} |
|
697
|
|
|
|
|
698
|
|
|
// 64-bit only functions? |
|
699
|
|
|
if (!function_exists('smf_crc32')) |
|
700
|
|
|
{ |
|
701
|
|
|
require_once $sourcedir . '/Subs-Compat.php'; |
|
702
|
|
|
} |
|
703
|
|
|
|
|
704
|
|
|
?> |
|
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.