|
1
|
|
|
<?php |
|
2
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
3
|
|
|
// GIF Util - (C) 2003 Yamasoft (S/C) |
|
4
|
|
|
// http://www.yamasoft.com |
|
5
|
|
|
// All Rights Reserved |
|
6
|
|
|
// This file can be freely copied, distributed, modified, updated by anyone under the only |
|
7
|
|
|
// condition to leave the original address (Yamasoft, http://www.yamasoft.com) and this header. |
|
8
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
9
|
|
|
// <gif> = gif_loadFile(filename, [index]) |
|
10
|
|
|
// <bool> = gif_getSize(<gif> or filename, &width, &height) |
|
11
|
|
|
// <bool> = gif_outputAsPng(<gif>, filename, [bgColor]) |
|
12
|
|
|
// <bool> = gif_outputAsBmp(<gif>, filename, [bgcolor]) |
|
13
|
|
|
// <bool> = gif_outputAsJpeg(<gif>, filename, [bgcolor]) - use cjpeg if available otherwise uses GD |
|
14
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
15
|
|
|
// Original code by Fabien Ezber |
|
16
|
|
|
// Modified by James Heinrich <[email protected]> for use in phpThumb() - December 10, 2003 |
|
17
|
|
|
// * Added function gif_loadFileToGDimageResource() - this returns a GD image resource |
|
18
|
|
|
// * Modified gif_outputAsJpeg() to check if it's running under Windows, or if cjpeg is not |
|
19
|
|
|
// available, in which case it will attempt to output JPEG using GD functions |
|
20
|
|
|
// * added @ error-suppression to two lines where it checks: if ($this->m_img->m_bTrans) |
|
21
|
|
|
// otherwise warnings are generated if error_reporting == E_ALL |
|
22
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23
|
|
|
|
|
24
|
|
|
function gif_loadFile($lpszFileName, $iIndex = 0) |
|
25
|
|
|
{ |
|
26
|
|
|
$gif = new CGIF(); |
|
27
|
|
|
if ($gif->loadFile($lpszFileName, $iIndex)) { |
|
28
|
|
|
return $gif; |
|
29
|
|
|
} |
|
30
|
|
|
return false; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
34
|
|
|
|
|
35
|
|
|
// Added by James Heinrich <[email protected]> - December 10, 2003 |
|
36
|
|
|
function gif_loadFileToGDimageResource($gifFilename, $bgColor = -1) |
|
37
|
|
|
{ |
|
38
|
|
|
if ($gif = gif_loadFile($gifFilename)) { |
|
39
|
|
|
if (!phpthumb_functions::FunctionIsDisabled('set_time_limit')) { |
|
40
|
|
|
// shouldn't take nearly this long |
|
41
|
|
|
set_time_limit(120); |
|
42
|
|
|
} |
|
43
|
|
|
// general strategy: convert raw data to PNG then convert PNG data to GD image resource |
|
44
|
|
|
$PNGdata = $gif->getPng($bgColor); |
|
45
|
|
|
if ($img = @imagecreatefromstring($PNGdata)) { |
|
|
|
|
|
|
46
|
|
|
// excellent - PNG image data successfully converted to GD image |
|
47
|
|
|
return $img; |
|
48
|
|
|
} elseif ($img = $gif->getGD_PixelPlotterVersion()) { |
|
49
|
|
|
// problem: imagecreatefromstring() didn't like the PNG image data. |
|
50
|
|
|
// This has been known to happen in PHP v4.0.6 |
|
51
|
|
|
// solution: take the raw image data and create a new GD image and plot |
|
52
|
|
|
// pixel-by-pixel on the GD image. This is extremely slow, but it does |
|
53
|
|
|
// work and a slow solution is better than no solution, right? :) |
|
54
|
|
|
return $img; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
61
|
|
|
|
|
62
|
|
|
function gif_outputAsBmp($gif, $lpszFileName, $bgColor = -1) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!isset($gif) || (@get_class($gif) <> 'cgif') || !$gif->loaded() || ($lpszFileName == '')) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$fd = $gif->getBmp($bgColor); |
|
69
|
|
|
if (strlen($fd) <= 0) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (!($fh = @fopen($lpszFileName, 'wb'))) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
@fwrite($fh, $fd, strlen($fd)); |
|
|
|
|
|
|
77
|
|
|
@fflush($fh); |
|
|
|
|
|
|
78
|
|
|
@fclose($fh); |
|
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
83
|
|
|
|
|
84
|
|
|
function gif_outputAsPng($gif, $lpszFileName, $bgColor = -1) |
|
85
|
|
|
{ |
|
86
|
|
|
if (!isset($gif) || (@get_class($gif) <> 'cgif') || !$gif->loaded() || ($lpszFileName == '')) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$fd = $gif->getPng($bgColor); |
|
91
|
|
|
if (strlen($fd) <= 0) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (!($fh = @fopen($lpszFileName, 'wb'))) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
@fwrite($fh, $fd, strlen($fd)); |
|
|
|
|
|
|
99
|
|
|
@fflush($fh); |
|
|
|
|
|
|
100
|
|
|
@fclose($fh); |
|
|
|
|
|
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
105
|
|
|
|
|
106
|
|
|
function gif_outputAsJpeg($gif, $lpszFileName, $bgColor = -1) |
|
107
|
|
|
{ |
|
108
|
|
|
// JPEG output that does not require cjpeg added by James Heinrich <[email protected]> - December 10, 2003 |
|
109
|
|
|
if ((strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') && (file_exists('/usr/local/bin/cjpeg') || shell_exec('which cjpeg'))) { |
|
110
|
|
|
if (gif_outputAsBmp($gif, $lpszFileName . '.bmp', $bgColor)) { |
|
111
|
|
|
exec('cjpeg ' . $lpszFileName . '.bmp >' . $lpszFileName . ' 2>/dev/null'); |
|
112
|
|
|
@unlink($lpszFileName . '.bmp'); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
if (@file_exists($lpszFileName)) { |
|
115
|
|
|
if (@filesize($lpszFileName) > 0) { |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
@unlink($lpszFileName); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} else { |
|
123
|
|
|
// either Windows, or cjpeg not found in path |
|
124
|
|
|
if ($img = @imagecreatefromstring($gif->getPng($bgColor))) { |
|
125
|
|
|
if (@imagejpeg($img, $lpszFileName)) { |
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
135
|
|
|
|
|
136
|
|
|
function gif_getSize($gif, &$width, &$height) |
|
137
|
|
|
{ |
|
138
|
|
|
if (isset($gif) && (@get_class($gif) == 'cgif') && $gif->loaded()) { |
|
139
|
|
|
$width = $gif->width(); |
|
140
|
|
|
$height = $gif->height(); |
|
141
|
|
|
} elseif (@file_exists($gif)) { |
|
142
|
|
|
$myGIF = new CGIF(); |
|
143
|
|
|
if (!$myGIF->getSize($gif, $width, $height)) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
} else { |
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
154
|
|
|
|
|
155
|
|
|
class CGIFLZW |
|
156
|
|
|
{ |
|
157
|
|
|
public $MAX_LZW_BITS; |
|
158
|
|
|
public $Fresh, $CodeSize, $SetCodeSize, $MaxCode, $MaxCodeSize, $FirstCode, $OldCode; |
|
159
|
|
|
public $ClearCode, $EndCode, $Next, $Vals, $Stack, $sp, $Buf, $CurBit, $LastBit, $Done, $LastByte; |
|
160
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
161
|
|
|
|
|
162
|
|
|
// CONSTRUCTOR |
|
163
|
|
|
public function __construct() |
|
164
|
|
|
{ |
|
165
|
|
|
$this->MAX_LZW_BITS = 12; |
|
166
|
|
|
unset($this->Next); |
|
167
|
|
|
unset($this->Vals); |
|
168
|
|
|
unset($this->Stack); |
|
169
|
|
|
unset($this->Buf); |
|
170
|
|
|
|
|
171
|
|
|
$this->Next = range(0, (1 << $this->MAX_LZW_BITS) - 1); |
|
172
|
|
|
$this->Vals = range(0, (1 << $this->MAX_LZW_BITS) - 1); |
|
173
|
|
|
$this->Stack = range(0, (1 << ($this->MAX_LZW_BITS + 1)) - 1); |
|
174
|
|
|
$this->Buf = range(0, 279); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
178
|
|
|
|
|
179
|
|
|
public function deCompress($data, &$datLen) |
|
180
|
|
|
{ |
|
181
|
|
|
$stLen = strlen($data); |
|
182
|
|
|
$datLen = 0; |
|
183
|
|
|
$ret = ''; |
|
184
|
|
|
|
|
185
|
|
|
// INITIALIZATION |
|
186
|
|
|
$this->LZWCommand($data, true); |
|
187
|
|
|
|
|
188
|
|
|
while (($iIndex = $this->LZWCommand($data, false)) >= 0) { |
|
189
|
|
|
$ret .= chr($iIndex); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$datLen = $stLen - strlen($data); |
|
193
|
|
|
|
|
194
|
|
|
if ($iIndex != -2) { |
|
195
|
|
|
return false; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $ret; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
202
|
|
|
|
|
203
|
|
|
public function LZWCommand(&$data, $bInit) |
|
204
|
|
|
{ |
|
205
|
|
|
if ($bInit) { |
|
206
|
|
|
$this->SetCodeSize = ord($data[0]); |
|
207
|
|
|
$data = substr($data, 1); |
|
208
|
|
|
|
|
209
|
|
|
$this->CodeSize = $this->SetCodeSize + 1; |
|
210
|
|
|
$this->ClearCode = 1 << $this->SetCodeSize; |
|
211
|
|
|
$this->EndCode = $this->ClearCode + 1; |
|
212
|
|
|
$this->MaxCode = $this->ClearCode + 2; |
|
213
|
|
|
$this->MaxCodeSize = $this->ClearCode << 1; |
|
214
|
|
|
|
|
215
|
|
|
$this->GetCode($data, $bInit); |
|
216
|
|
|
|
|
217
|
|
|
$this->Fresh = 1; |
|
218
|
|
|
for ($i = 0; $i < $this->ClearCode; $i++) { |
|
219
|
|
|
$this->Next[$i] = 0; |
|
220
|
|
|
$this->Vals[$i] = $i; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
for (; $i < (1 << $this->MAX_LZW_BITS); $i++) { |
|
224
|
|
|
$this->Next[$i] = 0; |
|
225
|
|
|
$this->Vals[$i] = 0; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$this->sp = 0; |
|
229
|
|
|
return 1; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
if ($this->Fresh) { |
|
233
|
|
|
$this->Fresh = 0; |
|
234
|
|
|
do { |
|
235
|
|
|
$this->FirstCode = $this->GetCode($data, $bInit); |
|
236
|
|
|
$this->OldCode = $this->FirstCode; |
|
237
|
|
|
} while ($this->FirstCode == $this->ClearCode); |
|
238
|
|
|
|
|
239
|
|
|
return $this->FirstCode; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
if ($this->sp > 0) { |
|
243
|
|
|
$this->sp--; |
|
244
|
|
|
return $this->Stack[$this->sp]; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
while (($Code = $this->GetCode($data, $bInit)) >= 0) { |
|
248
|
|
|
if ($Code == $this->ClearCode) { |
|
249
|
|
|
for ($i = 0; $i < $this->ClearCode; $i++) { |
|
250
|
|
|
$this->Next[$i] = 0; |
|
251
|
|
|
$this->Vals[$i] = $i; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
for (; $i < (1 << $this->MAX_LZW_BITS); $i++) { |
|
255
|
|
|
$this->Next[$i] = 0; |
|
256
|
|
|
$this->Vals[$i] = 0; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
$this->CodeSize = $this->SetCodeSize + 1; |
|
260
|
|
|
$this->MaxCodeSize = $this->ClearCode << 1; |
|
261
|
|
|
$this->MaxCode = $this->ClearCode + 2; |
|
262
|
|
|
$this->sp = 0; |
|
263
|
|
|
$this->FirstCode = $this->GetCode($data, $bInit); |
|
264
|
|
|
$this->OldCode = $this->FirstCode; |
|
265
|
|
|
|
|
266
|
|
|
return $this->FirstCode; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
if ($Code == $this->EndCode) { |
|
270
|
|
|
return -2; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
$InCode = $Code; |
|
274
|
|
|
if ($Code >= $this->MaxCode) { |
|
275
|
|
|
$this->Stack[$this->sp] = $this->FirstCode; |
|
276
|
|
|
$this->sp++; |
|
277
|
|
|
$Code = $this->OldCode; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
while ($Code >= $this->ClearCode) { |
|
281
|
|
|
$this->Stack[$this->sp] = $this->Vals[$Code]; |
|
282
|
|
|
$this->sp++; |
|
283
|
|
|
|
|
284
|
|
|
if ($Code == $this->Next[$Code]) // Circular table entry, big GIF Error! |
|
285
|
|
|
{ |
|
286
|
|
|
return -1; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$Code = $this->Next[$Code]; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
$this->FirstCode = $this->Vals[$Code]; |
|
293
|
|
|
$this->Stack[$this->sp] = $this->FirstCode; |
|
294
|
|
|
$this->sp++; |
|
295
|
|
|
|
|
296
|
|
|
if (($Code = $this->MaxCode) < (1 << $this->MAX_LZW_BITS)) { |
|
297
|
|
|
$this->Next[$Code] = $this->OldCode; |
|
298
|
|
|
$this->Vals[$Code] = $this->FirstCode; |
|
299
|
|
|
$this->MaxCode++; |
|
300
|
|
|
|
|
301
|
|
|
if (($this->MaxCode >= $this->MaxCodeSize) && ($this->MaxCodeSize < (1 << $this->MAX_LZW_BITS))) { |
|
302
|
|
|
$this->MaxCodeSize *= 2; |
|
303
|
|
|
$this->CodeSize++; |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$this->OldCode = $InCode; |
|
308
|
|
|
if ($this->sp > 0) { |
|
309
|
|
|
$this->sp--; |
|
310
|
|
|
return $this->Stack[$this->sp]; |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
return $Code; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
318
|
|
|
|
|
319
|
|
|
public function GetCode(&$data, $bInit) |
|
320
|
|
|
{ |
|
321
|
|
|
if ($bInit) { |
|
322
|
|
|
$this->CurBit = 0; |
|
323
|
|
|
$this->LastBit = 0; |
|
324
|
|
|
$this->Done = 0; |
|
325
|
|
|
$this->LastByte = 2; |
|
326
|
|
|
return 1; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
if (($this->CurBit + $this->CodeSize) >= $this->LastBit) { |
|
330
|
|
|
if ($this->Done) { |
|
331
|
|
|
if ($this->CurBit >= $this->LastBit) { |
|
332
|
|
|
// Ran off the end of my bits |
|
333
|
|
|
return 0; |
|
334
|
|
|
} |
|
335
|
|
|
return -1; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
$this->Buf[0] = $this->Buf[$this->LastByte - 2]; |
|
339
|
|
|
$this->Buf[1] = $this->Buf[$this->LastByte - 1]; |
|
340
|
|
|
|
|
341
|
|
|
$Count = ord($data[0]); |
|
342
|
|
|
$data = substr($data, 1); |
|
343
|
|
|
|
|
344
|
|
|
if ($Count) { |
|
345
|
|
|
for ($i = 0; $i < $Count; $i++) { |
|
346
|
|
|
$this->Buf[2 + $i] = ord($data[$i]); |
|
347
|
|
|
} |
|
348
|
|
|
$data = substr($data, $Count); |
|
349
|
|
|
} else { |
|
350
|
|
|
$this->Done = 1; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
$this->LastByte = 2 + $Count; |
|
354
|
|
|
$this->CurBit = ($this->CurBit - $this->LastBit) + 16; |
|
355
|
|
|
$this->LastBit = (2 + $Count) << 3; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
$iRet = 0; |
|
359
|
|
|
for ($i = $this->CurBit, $j = 0; $j < $this->CodeSize; $i++, $j++) { |
|
360
|
|
|
$iRet |= (($this->Buf[(int)($i / 8)] & (1 << ($i % 8))) != 0) << $j; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
$this->CurBit += $this->CodeSize; |
|
364
|
|
|
return $iRet; |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
369
|
|
|
|
|
370
|
|
|
class CGIFCOLORTABLE |
|
371
|
|
|
{ |
|
372
|
|
|
public $m_nColors; |
|
373
|
|
|
public $m_arColors; |
|
374
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
375
|
|
|
|
|
376
|
|
|
// CONSTRUCTOR |
|
377
|
|
|
public function __construct() |
|
378
|
|
|
{ |
|
379
|
|
|
unset($this->m_nColors); |
|
380
|
|
|
unset($this->m_arColors); |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
384
|
|
|
|
|
385
|
|
|
public function load($lpData, $num) |
|
386
|
|
|
{ |
|
387
|
|
|
$this->m_nColors = 0; |
|
388
|
|
|
$this->m_arColors = []; |
|
389
|
|
|
|
|
390
|
|
|
for ($i = 0; $i < $num; $i++) { |
|
391
|
|
|
$rgb = substr($lpData, $i * 3, 3); |
|
392
|
|
|
if (strlen($rgb) < 3) { |
|
393
|
|
|
return false; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
$this->m_arColors[] = (ord($rgb[2]) << 16) + (ord($rgb[1]) << 8) + ord($rgb[0]); |
|
397
|
|
|
$this->m_nColors++; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
return true; |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
404
|
|
|
|
|
405
|
|
|
public function toString() |
|
406
|
|
|
{ |
|
407
|
|
|
$ret = ''; |
|
408
|
|
|
|
|
409
|
|
|
for ($i = 0; $i < $this->m_nColors; $i++) { |
|
410
|
|
|
$ret .= chr($this->m_arColors[$i] & 0x000000FF) . // R |
|
411
|
|
|
chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G |
|
412
|
|
|
chr(($this->m_arColors[$i] & 0x00FF0000) >> 16); // B |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
return $ret; |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
419
|
|
|
|
|
420
|
|
|
public function toRGBQuad() |
|
421
|
|
|
{ |
|
422
|
|
|
$ret = ''; |
|
423
|
|
|
|
|
424
|
|
|
for ($i = 0; $i < $this->m_nColors; $i++) { |
|
425
|
|
|
$ret .= chr(($this->m_arColors[$i] & 0x00FF0000) >> 16) . // B |
|
426
|
|
|
chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G |
|
427
|
|
|
chr($this->m_arColors[$i] & 0x000000FF) . // R |
|
428
|
|
|
"\x00"; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
return $ret; |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
435
|
|
|
|
|
436
|
|
|
public function colorIndex($rgb) |
|
437
|
|
|
{ |
|
438
|
|
|
$rgb = (int)$rgb & 0xFFFFFF; |
|
439
|
|
|
$r1 = ($rgb & 0x0000FF); |
|
440
|
|
|
$g1 = ($rgb & 0x00FF00) >> 8; |
|
441
|
|
|
$b1 = ($rgb & 0xFF0000) >> 16; |
|
442
|
|
|
$idx = -1; |
|
443
|
|
|
$dif = 0; |
|
444
|
|
|
|
|
445
|
|
|
for ($i = 0; $i < $this->m_nColors; $i++) { |
|
446
|
|
|
$r2 = ($this->m_arColors[$i] & 0x000000FF); |
|
447
|
|
|
$g2 = ($this->m_arColors[$i] & 0x0000FF00) >> 8; |
|
448
|
|
|
$b2 = ($this->m_arColors[$i] & 0x00FF0000) >> 16; |
|
449
|
|
|
$d = abs($r2 - $r1) + abs($g2 - $g1) + abs($b2 - $b1); |
|
450
|
|
|
|
|
451
|
|
|
if (($idx == -1) || ($d < $dif)) { |
|
452
|
|
|
$idx = $i; |
|
453
|
|
|
$dif = $d; |
|
454
|
|
|
} |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
return $idx; |
|
458
|
|
|
} |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
462
|
|
|
|
|
463
|
|
|
class CGIFFILEHEADER |
|
464
|
|
|
{ |
|
465
|
|
|
public $m_lpVer; |
|
466
|
|
|
public $m_nWidth; |
|
467
|
|
|
public $m_nHeight; |
|
468
|
|
|
public $m_bGlobalClr; |
|
469
|
|
|
public $m_nColorRes; |
|
470
|
|
|
public $m_bSorted; |
|
471
|
|
|
public $m_nTableSize; |
|
472
|
|
|
public $m_nBgColor; |
|
473
|
|
|
public $m_nPixelRatio; |
|
474
|
|
|
public $m_colorTable; |
|
475
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
476
|
|
|
|
|
477
|
|
|
// CONSTRUCTOR |
|
478
|
|
|
public function __construct() |
|
479
|
|
|
{ |
|
480
|
|
|
unset($this->m_lpVer); |
|
481
|
|
|
unset($this->m_nWidth); |
|
482
|
|
|
unset($this->m_nHeight); |
|
483
|
|
|
unset($this->m_bGlobalClr); |
|
484
|
|
|
unset($this->m_nColorRes); |
|
485
|
|
|
unset($this->m_bSorted); |
|
486
|
|
|
unset($this->m_nTableSize); |
|
487
|
|
|
unset($this->m_nBgColor); |
|
488
|
|
|
unset($this->m_nPixelRatio); |
|
489
|
|
|
unset($this->m_colorTable); |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
493
|
|
|
|
|
494
|
|
|
public function load($lpData, &$hdrLen) |
|
495
|
|
|
{ |
|
496
|
|
|
$hdrLen = 0; |
|
497
|
|
|
|
|
498
|
|
|
$this->m_lpVer = substr($lpData, 0, 6); |
|
499
|
|
|
if (($this->m_lpVer <> 'GIF87a') && ($this->m_lpVer <> 'GIF89a')) { |
|
500
|
|
|
return false; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
$this->m_nWidth = $this->w2i(substr($lpData, 6, 2)); |
|
504
|
|
|
$this->m_nHeight = $this->w2i(substr($lpData, 8, 2)); |
|
505
|
|
|
if (!$this->m_nWidth || !$this->m_nHeight) { |
|
506
|
|
|
return false; |
|
507
|
|
|
} |
|
508
|
|
|
|
|
509
|
|
|
$b = ord($lpData[10]); |
|
510
|
|
|
$this->m_bGlobalClr = ($b & 0x80) ? true : false; |
|
511
|
|
|
$this->m_nColorRes = ($b & 0x70) >> 4; |
|
512
|
|
|
$this->m_bSorted = ($b & 0x08) ? true : false; |
|
513
|
|
|
$this->m_nTableSize = 2 << ($b & 0x07); |
|
514
|
|
|
$this->m_nBgColor = ord($lpData[11]); |
|
515
|
|
|
$this->m_nPixelRatio = ord($lpData[12]); |
|
516
|
|
|
$hdrLen = 13; |
|
517
|
|
|
|
|
518
|
|
|
if ($this->m_bGlobalClr) { |
|
519
|
|
|
$this->m_colorTable = new CGIFCOLORTABLE(); |
|
520
|
|
|
if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) { |
|
521
|
|
|
return false; |
|
522
|
|
|
} |
|
523
|
|
|
$hdrLen += 3 * $this->m_nTableSize; |
|
524
|
|
|
} |
|
525
|
|
|
|
|
526
|
|
|
return true; |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
530
|
|
|
|
|
531
|
|
|
public function w2i($str) |
|
532
|
|
|
{ |
|
533
|
|
|
return ord($str[0]) + (ord($str[1]) << 8); |
|
534
|
|
|
} |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
538
|
|
|
|
|
539
|
|
|
class CGIFIMAGEHEADER |
|
540
|
|
|
{ |
|
541
|
|
|
public $m_nLeft; |
|
542
|
|
|
public $m_nTop; |
|
543
|
|
|
public $m_nWidth; |
|
544
|
|
|
public $m_nHeight; |
|
545
|
|
|
public $m_bLocalClr; |
|
546
|
|
|
public $m_bInterlace; |
|
547
|
|
|
public $m_bSorted; |
|
548
|
|
|
public $m_nTableSize; |
|
549
|
|
|
public $m_colorTable; |
|
550
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
551
|
|
|
|
|
552
|
|
|
// CONSTRUCTOR |
|
553
|
|
|
public function __construct() |
|
554
|
|
|
{ |
|
555
|
|
|
unset($this->m_nLeft); |
|
556
|
|
|
unset($this->m_nTop); |
|
557
|
|
|
unset($this->m_nWidth); |
|
558
|
|
|
unset($this->m_nHeight); |
|
559
|
|
|
unset($this->m_bLocalClr); |
|
560
|
|
|
unset($this->m_bInterlace); |
|
561
|
|
|
unset($this->m_bSorted); |
|
562
|
|
|
unset($this->m_nTableSize); |
|
563
|
|
|
unset($this->m_colorTable); |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
567
|
|
|
|
|
568
|
|
|
public function load($lpData, &$hdrLen) |
|
569
|
|
|
{ |
|
570
|
|
|
$hdrLen = 0; |
|
571
|
|
|
|
|
572
|
|
|
$this->m_nLeft = $this->w2i(substr($lpData, 0, 2)); |
|
573
|
|
|
$this->m_nTop = $this->w2i(substr($lpData, 2, 2)); |
|
574
|
|
|
$this->m_nWidth = $this->w2i(substr($lpData, 4, 2)); |
|
575
|
|
|
$this->m_nHeight = $this->w2i(substr($lpData, 6, 2)); |
|
576
|
|
|
|
|
577
|
|
|
if (!$this->m_nWidth || !$this->m_nHeight) { |
|
578
|
|
|
return false; |
|
579
|
|
|
} |
|
580
|
|
|
|
|
581
|
|
|
$b = ord($lpData[8]); |
|
582
|
|
|
$this->m_bLocalClr = ($b & 0x80) ? true : false; |
|
583
|
|
|
$this->m_bInterlace = ($b & 0x40) ? true : false; |
|
584
|
|
|
$this->m_bSorted = ($b & 0x20) ? true : false; |
|
585
|
|
|
$this->m_nTableSize = 2 << ($b & 0x07); |
|
586
|
|
|
$hdrLen = 9; |
|
587
|
|
|
|
|
588
|
|
|
if ($this->m_bLocalClr) { |
|
589
|
|
|
$this->m_colorTable = new CGIFCOLORTABLE(); |
|
590
|
|
|
if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) { |
|
591
|
|
|
return false; |
|
592
|
|
|
} |
|
593
|
|
|
$hdrLen += 3 * $this->m_nTableSize; |
|
594
|
|
|
} |
|
595
|
|
|
|
|
596
|
|
|
return true; |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
600
|
|
|
|
|
601
|
|
|
public function w2i($str) |
|
602
|
|
|
{ |
|
603
|
|
|
return ord($str[0]) + (ord($str[1]) << 8); |
|
604
|
|
|
} |
|
605
|
|
|
} |
|
606
|
|
|
|
|
607
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
608
|
|
|
|
|
609
|
|
|
class CGIFIMAGE |
|
610
|
|
|
{ |
|
611
|
|
|
public $m_disp; |
|
612
|
|
|
public $m_bUser; |
|
613
|
|
|
public $m_bTrans; |
|
614
|
|
|
public $m_nDelay; |
|
615
|
|
|
public $m_nTrans; |
|
616
|
|
|
public $m_lpComm; |
|
617
|
|
|
public $m_gih; |
|
618
|
|
|
public $m_data; |
|
619
|
|
|
public $m_lzw; |
|
620
|
|
|
|
|
621
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
622
|
|
|
|
|
623
|
|
|
public function __construct() |
|
624
|
|
|
{ |
|
625
|
|
|
unset($this->m_disp); |
|
626
|
|
|
unset($this->m_bUser); |
|
627
|
|
|
unset($this->m_bTrans); |
|
628
|
|
|
unset($this->m_nDelay); |
|
629
|
|
|
unset($this->m_nTrans); |
|
630
|
|
|
unset($this->m_lpComm); |
|
631
|
|
|
unset($this->m_data); |
|
632
|
|
|
$this->m_gih = new CGIFIMAGEHEADER(); |
|
633
|
|
|
$this->m_lzw = new CGIFLZW(); |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
637
|
|
|
|
|
638
|
|
|
public function load($data, &$datLen) |
|
639
|
|
|
{ |
|
640
|
|
|
$datLen = 0; |
|
641
|
|
|
|
|
642
|
|
|
while (true) { |
|
643
|
|
|
$b = ord($data[0]); |
|
644
|
|
|
$data = substr($data, 1); |
|
645
|
|
|
$datLen++; |
|
646
|
|
|
|
|
647
|
|
|
switch ($b) { |
|
648
|
|
|
case 0x21: // Extension |
|
649
|
|
|
if (!$this->skipExt($data, $len = 0)) { |
|
|
|
|
|
|
650
|
|
|
return false; |
|
651
|
|
|
} |
|
652
|
|
|
$datLen += $len; |
|
653
|
|
|
break; |
|
654
|
|
|
|
|
655
|
|
|
case 0x2C: // Image |
|
656
|
|
|
// LOAD HEADER & COLOR TABLE |
|
657
|
|
|
if (!$this->m_gih->load($data, $len = 0)) { |
|
|
|
|
|
|
658
|
|
|
return false; |
|
659
|
|
|
} |
|
660
|
|
|
$data = substr($data, $len); |
|
661
|
|
|
$datLen += $len; |
|
662
|
|
|
|
|
663
|
|
|
// ALLOC BUFFER |
|
664
|
|
|
if (!($this->m_data = $this->m_lzw->deCompress($data, $len = 0))) { |
|
|
|
|
|
|
665
|
|
|
return false; |
|
666
|
|
|
} |
|
667
|
|
|
$data = substr($data, $len); |
|
|
|
|
|
|
668
|
|
|
$datLen += $len; |
|
669
|
|
|
|
|
670
|
|
|
if ($this->m_gih->m_bInterlace) { |
|
671
|
|
|
$this->deInterlace(); |
|
672
|
|
|
} |
|
673
|
|
|
return true; |
|
674
|
|
|
|
|
675
|
|
|
case 0x3B: // EOF |
|
676
|
|
|
default: |
|
677
|
|
|
return false; |
|
678
|
|
|
} |
|
679
|
|
|
} |
|
680
|
|
|
return false; |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
684
|
|
|
|
|
685
|
|
|
public function skipExt(&$data, &$extLen) |
|
686
|
|
|
{ |
|
687
|
|
|
$extLen = 0; |
|
688
|
|
|
|
|
689
|
|
|
$b = ord($data[0]); |
|
690
|
|
|
$data = substr($data, 1); |
|
691
|
|
|
$extLen++; |
|
692
|
|
|
|
|
693
|
|
|
switch ($b) { |
|
694
|
|
|
case 0xF9: // Graphic Control |
|
695
|
|
|
$b = ord($data[1]); |
|
696
|
|
|
$this->m_disp = ($b & 0x1C) >> 2; |
|
697
|
|
|
$this->m_bUser = ($b & 0x02) ? true : false; |
|
698
|
|
|
$this->m_bTrans = ($b & 0x01) ? true : false; |
|
699
|
|
|
$this->m_nDelay = $this->w2i(substr($data, 2, 2)); |
|
700
|
|
|
$this->m_nTrans = ord($data[4]); |
|
701
|
|
|
break; |
|
702
|
|
|
|
|
703
|
|
|
case 0xFE: // Comment |
|
704
|
|
|
$this->m_lpComm = substr($data, 1, ord($data[0])); |
|
705
|
|
|
break; |
|
706
|
|
|
|
|
707
|
|
|
case 0x01: // Plain text |
|
708
|
|
|
break; |
|
709
|
|
|
|
|
710
|
|
|
case 0xFF: // Application |
|
711
|
|
|
break; |
|
712
|
|
|
} |
|
713
|
|
|
|
|
714
|
|
|
// SKIP DEFAULT AS DEFS MAY CHANGE |
|
715
|
|
|
$b = ord($data[0]); |
|
716
|
|
|
$data = substr($data, 1); |
|
717
|
|
|
$extLen++; |
|
718
|
|
|
while ($b > 0) { |
|
719
|
|
|
$data = substr($data, $b); |
|
720
|
|
|
$extLen += $b; |
|
721
|
|
|
$b = ord($data[0]); |
|
722
|
|
|
$data = substr($data, 1); |
|
723
|
|
|
$extLen++; |
|
724
|
|
|
} |
|
725
|
|
|
return true; |
|
726
|
|
|
} |
|
727
|
|
|
|
|
728
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
729
|
|
|
|
|
730
|
|
|
public function w2i($str) |
|
731
|
|
|
{ |
|
732
|
|
|
return ord($str[0]) + (ord($str[1]) << 8); |
|
733
|
|
|
} |
|
734
|
|
|
|
|
735
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
736
|
|
|
|
|
737
|
|
|
public function deInterlace() |
|
738
|
|
|
{ |
|
739
|
|
|
$data = $this->m_data; |
|
740
|
|
|
$s = 0; |
|
741
|
|
|
$y = 0; |
|
742
|
|
|
|
|
743
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
744
|
|
|
switch ($i) { |
|
745
|
|
|
case 0: |
|
746
|
|
|
$s = 8; |
|
747
|
|
|
$y = 0; |
|
748
|
|
|
break; |
|
749
|
|
|
|
|
750
|
|
|
case 1: |
|
751
|
|
|
$s = 8; |
|
752
|
|
|
$y = 4; |
|
753
|
|
|
break; |
|
754
|
|
|
|
|
755
|
|
|
case 2: |
|
756
|
|
|
$s = 4; |
|
757
|
|
|
$y = 2; |
|
758
|
|
|
break; |
|
759
|
|
|
|
|
760
|
|
|
case 3: |
|
761
|
|
|
$s = 2; |
|
762
|
|
|
$y = 1; |
|
763
|
|
|
break; |
|
764
|
|
|
} |
|
765
|
|
|
|
|
766
|
|
|
for (; $y < $this->m_gih->m_nHeight; $y += $s) { |
|
767
|
|
|
$lne = substr($this->m_data, 0, $this->m_gih->m_nWidth); |
|
768
|
|
|
$this->m_data = substr($this->m_data, $this->m_gih->m_nWidth); |
|
769
|
|
|
|
|
770
|
|
|
$data = substr($data, 0, $y * $this->m_gih->m_nWidth) . $lne . substr($data, ($y + 1) * $this->m_gih->m_nWidth); |
|
771
|
|
|
} |
|
772
|
|
|
} |
|
773
|
|
|
|
|
774
|
|
|
$this->m_data = $data; |
|
775
|
|
|
} |
|
776
|
|
|
} |
|
777
|
|
|
|
|
778
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
779
|
|
|
|
|
780
|
|
|
class CGIF |
|
781
|
|
|
{ |
|
782
|
|
|
public $m_gfh; |
|
783
|
|
|
public $m_lpData; |
|
784
|
|
|
public $m_img; |
|
785
|
|
|
public $m_bLoaded; |
|
786
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
787
|
|
|
|
|
788
|
|
|
// CONSTRUCTOR |
|
789
|
|
|
public function __construct() |
|
790
|
|
|
{ |
|
791
|
|
|
$this->m_gfh = new CGIFFILEHEADER(); |
|
792
|
|
|
$this->m_img = new CGIFIMAGE(); |
|
793
|
|
|
$this->m_lpData = ''; |
|
794
|
|
|
$this->m_bLoaded = false; |
|
795
|
|
|
} |
|
796
|
|
|
|
|
797
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
798
|
|
|
|
|
799
|
|
|
public function loadFile($lpszFileName, $iIndex) |
|
800
|
|
|
{ |
|
801
|
|
|
if ($iIndex < 0) { |
|
802
|
|
|
return false; |
|
803
|
|
|
} |
|
804
|
|
|
|
|
805
|
|
|
// READ FILE |
|
806
|
|
|
if (!($fh = @fopen($lpszFileName, 'rb'))) { |
|
807
|
|
|
return false; |
|
808
|
|
|
} |
|
809
|
|
|
$this->m_lpData = @fread($fh, @filesize($lpszFileName)); |
|
|
|
|
|
|
810
|
|
|
fclose($fh); |
|
811
|
|
|
|
|
812
|
|
|
// GET FILE HEADER |
|
813
|
|
|
if (!$this->m_gfh->load($this->m_lpData, $len = 0)) { |
|
|
|
|
|
|
814
|
|
|
return false; |
|
815
|
|
|
} |
|
816
|
|
|
$this->m_lpData = substr($this->m_lpData, $len); |
|
|
|
|
|
|
817
|
|
|
|
|
818
|
|
|
do { |
|
819
|
|
|
if (!$this->m_img->load($this->m_lpData, $imgLen = 0)) { |
|
|
|
|
|
|
820
|
|
|
return false; |
|
821
|
|
|
} |
|
822
|
|
|
$this->m_lpData = substr($this->m_lpData, $imgLen); |
|
823
|
|
|
} while ($iIndex-- > 0); |
|
824
|
|
|
|
|
825
|
|
|
$this->m_bLoaded = true; |
|
826
|
|
|
return true; |
|
827
|
|
|
} |
|
828
|
|
|
|
|
829
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
830
|
|
|
|
|
831
|
|
|
public function getSize($lpszFileName, &$width, &$height) |
|
832
|
|
|
{ |
|
833
|
|
|
if (!($fh = @fopen($lpszFileName, 'rb'))) { |
|
834
|
|
|
return false; |
|
835
|
|
|
} |
|
836
|
|
|
$data = @fread($fh, @filesize($lpszFileName)); |
|
|
|
|
|
|
837
|
|
|
@fclose($fh); |
|
|
|
|
|
|
838
|
|
|
|
|
839
|
|
|
$gfh = new CGIFFILEHEADER(); |
|
840
|
|
|
if (!$gfh->load($data, $len = 0)) { |
|
|
|
|
|
|
841
|
|
|
return false; |
|
842
|
|
|
} |
|
843
|
|
|
|
|
844
|
|
|
$width = $gfh->m_nWidth; |
|
845
|
|
|
$height = $gfh->m_nHeight; |
|
846
|
|
|
return true; |
|
847
|
|
|
} |
|
848
|
|
|
|
|
849
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
850
|
|
|
|
|
851
|
|
|
public function getBmp($bgColor) |
|
852
|
|
|
{ |
|
853
|
|
|
$out = ''; |
|
854
|
|
|
|
|
855
|
|
|
if (!$this->m_bLoaded) { |
|
856
|
|
|
return false; |
|
857
|
|
|
} |
|
858
|
|
|
|
|
859
|
|
|
// PREPARE COLOR TABLE (RGBQUADs) |
|
860
|
|
|
if ($this->m_img->m_gih->m_bLocalClr) { |
|
861
|
|
|
$nColors = $this->m_img->m_gih->m_nTableSize; |
|
862
|
|
|
$rgbq = $this->m_img->m_gih->m_colorTable->toRGBQuad(); |
|
863
|
|
|
if ($bgColor != -1) { |
|
864
|
|
|
$bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor); |
|
865
|
|
|
} |
|
866
|
|
|
} elseif ($this->m_gfh->m_bGlobalClr) { |
|
867
|
|
|
$nColors = $this->m_gfh->m_nTableSize; |
|
868
|
|
|
$rgbq = $this->m_gfh->m_colorTable->toRGBQuad(); |
|
869
|
|
|
if ($bgColor != -1) { |
|
870
|
|
|
$bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor); |
|
871
|
|
|
} |
|
872
|
|
|
} else { |
|
873
|
|
|
$nColors = 0; |
|
874
|
|
|
$rgbq = ''; |
|
875
|
|
|
$bgColor = -1; |
|
876
|
|
|
} |
|
877
|
|
|
|
|
878
|
|
|
// PREPARE BITMAP BITS |
|
879
|
|
|
$data = $this->m_img->m_data; |
|
880
|
|
|
$nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth; |
|
881
|
|
|
$bmp = ''; |
|
882
|
|
|
$nPad = ($this->m_gfh->m_nWidth % 4) ? 4 - ($this->m_gfh->m_nWidth % 4) : 0; |
|
883
|
|
|
for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { |
|
884
|
|
|
for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { |
|
885
|
|
|
if (($x >= $this->m_img->m_gih->m_nLeft) |
|
886
|
|
|
&& ($y >= $this->m_img->m_gih->m_nTop) |
|
887
|
|
|
&& ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) |
|
888
|
|
|
&& ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) { |
|
889
|
|
|
// PART OF IMAGE |
|
890
|
|
|
if (@$this->m_img->m_bTrans && (ord($data[$nPxl]) == $this->m_img->m_nTrans)) { |
|
891
|
|
|
// TRANSPARENT -> BACKGROUND |
|
892
|
|
|
if ($bgColor == -1) { |
|
893
|
|
|
$bmp .= chr($this->m_gfh->m_nBgColor); |
|
894
|
|
|
} else { |
|
895
|
|
|
$bmp .= chr($bgColor); |
|
896
|
|
|
} |
|
897
|
|
|
} else { |
|
898
|
|
|
$bmp .= $data[$nPxl]; |
|
899
|
|
|
} |
|
900
|
|
|
} else { |
|
901
|
|
|
// BACKGROUND |
|
902
|
|
|
if ($bgColor == -1) { |
|
903
|
|
|
$bmp .= chr($this->m_gfh->m_nBgColor); |
|
904
|
|
|
} else { |
|
905
|
|
|
$bmp .= chr($bgColor); |
|
906
|
|
|
} |
|
907
|
|
|
} |
|
908
|
|
|
} |
|
909
|
|
|
$nPxl -= $this->m_gfh->m_nWidth << 1; |
|
910
|
|
|
|
|
911
|
|
|
// ADD PADDING |
|
912
|
|
|
for ($x = 0; $x < $nPad; $x++) { |
|
913
|
|
|
$bmp .= "\x00"; |
|
914
|
|
|
} |
|
915
|
|
|
} |
|
916
|
|
|
|
|
917
|
|
|
// BITMAPFILEHEADER |
|
918
|
|
|
$out .= 'BM'; |
|
919
|
|
|
$out .= $this->dword(14 + 40 + ($nColors << 2) + strlen($bmp)); |
|
920
|
|
|
$out .= "\x00\x00"; |
|
921
|
|
|
$out .= "\x00\x00"; |
|
922
|
|
|
$out .= $this->dword(14 + 40 + ($nColors << 2)); |
|
923
|
|
|
|
|
924
|
|
|
// BITMAPINFOHEADER |
|
925
|
|
|
$out .= $this->dword(40); |
|
926
|
|
|
$out .= $this->dword($this->m_gfh->m_nWidth); |
|
927
|
|
|
$out .= $this->dword($this->m_gfh->m_nHeight); |
|
928
|
|
|
$out .= "\x01\x00"; |
|
929
|
|
|
$out .= "\x08\x00"; |
|
930
|
|
|
$out .= "\x00\x00\x00\x00"; |
|
931
|
|
|
$out .= "\x00\x00\x00\x00"; |
|
932
|
|
|
$out .= "\x12\x0B\x00\x00"; |
|
933
|
|
|
$out .= "\x12\x0B\x00\x00"; |
|
934
|
|
|
$out .= $this->dword($nColors % 256); |
|
935
|
|
|
$out .= "\x00\x00\x00\x00"; |
|
936
|
|
|
|
|
937
|
|
|
// COLOR TABLE |
|
938
|
|
|
if ($nColors > 0) { |
|
939
|
|
|
$out .= $rgbq; |
|
940
|
|
|
} |
|
941
|
|
|
|
|
942
|
|
|
// DATA |
|
943
|
|
|
$out .= $bmp; |
|
944
|
|
|
|
|
945
|
|
|
return $out; |
|
946
|
|
|
} |
|
947
|
|
|
|
|
948
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
949
|
|
|
|
|
950
|
|
|
public function getPng($bgColor) |
|
951
|
|
|
{ |
|
952
|
|
|
$out = ''; |
|
953
|
|
|
|
|
954
|
|
|
if (!$this->m_bLoaded) { |
|
955
|
|
|
return false; |
|
956
|
|
|
} |
|
957
|
|
|
|
|
958
|
|
|
// PREPARE COLOR TABLE (RGBQUADs) |
|
959
|
|
|
if ($this->m_img->m_gih->m_bLocalClr) { |
|
960
|
|
|
$nColors = $this->m_img->m_gih->m_nTableSize; |
|
961
|
|
|
$pal = $this->m_img->m_gih->m_colorTable->toString(); |
|
962
|
|
|
if ($bgColor != -1) { |
|
963
|
|
|
$bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor); |
|
964
|
|
|
} |
|
965
|
|
|
} elseif ($this->m_gfh->m_bGlobalClr) { |
|
966
|
|
|
$nColors = $this->m_gfh->m_nTableSize; |
|
967
|
|
|
$pal = $this->m_gfh->m_colorTable->toString(); |
|
968
|
|
|
if ($bgColor != -1) { |
|
969
|
|
|
$bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor); |
|
970
|
|
|
} |
|
971
|
|
|
} else { |
|
972
|
|
|
$nColors = 0; |
|
973
|
|
|
$pal = ''; |
|
974
|
|
|
$bgColor = -1; |
|
975
|
|
|
} |
|
976
|
|
|
|
|
977
|
|
|
// PREPARE BITMAP BITS |
|
978
|
|
|
$data = $this->m_img->m_data; |
|
979
|
|
|
$nPxl = 0; |
|
980
|
|
|
$bmp = ''; |
|
981
|
|
|
for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { |
|
982
|
|
|
$bmp .= "\x00"; |
|
983
|
|
|
for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { |
|
984
|
|
|
if (($x >= $this->m_img->m_gih->m_nLeft) |
|
985
|
|
|
&& ($y >= $this->m_img->m_gih->m_nTop) |
|
986
|
|
|
&& ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) |
|
987
|
|
|
&& ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) { |
|
988
|
|
|
// PART OF IMAGE |
|
989
|
|
|
$bmp .= $data[$nPxl]; |
|
990
|
|
|
} else { |
|
991
|
|
|
// BACKGROUND |
|
992
|
|
|
if ($bgColor == -1) { |
|
993
|
|
|
$bmp .= chr($this->m_gfh->m_nBgColor); |
|
994
|
|
|
} else { |
|
995
|
|
|
$bmp .= chr($bgColor); |
|
996
|
|
|
} |
|
997
|
|
|
} |
|
998
|
|
|
} |
|
999
|
|
|
} |
|
1000
|
|
|
$bmp = gzcompress($bmp, 9); |
|
1001
|
|
|
|
|
1002
|
|
|
/////////////////////////////////////////////////////////////////////// |
|
1003
|
|
|
// SIGNATURE |
|
1004
|
|
|
$out .= "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"; |
|
1005
|
|
|
/////////////////////////////////////////////////////////////////////// |
|
1006
|
|
|
// HEADER |
|
1007
|
|
|
$out .= "\x00\x00\x00\x0D"; |
|
1008
|
|
|
$tmp = 'IHDR'; |
|
1009
|
|
|
$tmp .= $this->ndword($this->m_gfh->m_nWidth); |
|
1010
|
|
|
$tmp .= $this->ndword($this->m_gfh->m_nHeight); |
|
1011
|
|
|
$tmp .= "\x08\x03\x00\x00\x00"; |
|
1012
|
|
|
$out .= $tmp; |
|
1013
|
|
|
$out .= $this->ndword(crc32($tmp)); |
|
1014
|
|
|
/////////////////////////////////////////////////////////////////////// |
|
1015
|
|
|
// PALETTE |
|
1016
|
|
|
if ($nColors > 0) { |
|
1017
|
|
|
$out .= $this->ndword($nColors * 3); |
|
1018
|
|
|
$tmp = 'PLTE'; |
|
1019
|
|
|
$tmp .= $pal; |
|
1020
|
|
|
$out .= $tmp; |
|
1021
|
|
|
$out .= $this->ndword(crc32($tmp)); |
|
1022
|
|
|
} |
|
1023
|
|
|
/////////////////////////////////////////////////////////////////////// |
|
1024
|
|
|
// TRANSPARENCY |
|
1025
|
|
|
if (@$this->m_img->m_bTrans && ($nColors > 0)) { |
|
1026
|
|
|
$out .= $this->ndword($nColors); |
|
1027
|
|
|
$tmp = 'tRNS'; |
|
1028
|
|
|
for ($i = 0; $i < $nColors; $i++) { |
|
1029
|
|
|
$tmp .= ($i == $this->m_img->m_nTrans) ? "\x00" : "\xFF"; |
|
1030
|
|
|
} |
|
1031
|
|
|
$out .= $tmp; |
|
1032
|
|
|
$out .= $this->ndword(crc32($tmp)); |
|
1033
|
|
|
} |
|
1034
|
|
|
/////////////////////////////////////////////////////////////////////// |
|
1035
|
|
|
// DATA BITS |
|
1036
|
|
|
$out .= $this->ndword(strlen($bmp)); |
|
1037
|
|
|
$tmp = 'IDAT'; |
|
1038
|
|
|
$tmp .= $bmp; |
|
1039
|
|
|
$out .= $tmp; |
|
1040
|
|
|
$out .= $this->ndword(crc32($tmp)); |
|
1041
|
|
|
/////////////////////////////////////////////////////////////////////// |
|
1042
|
|
|
// END OF FILE |
|
1043
|
|
|
$out .= "\x00\x00\x00\x00IEND\xAE\x42\x60\x82"; |
|
1044
|
|
|
|
|
1045
|
|
|
return $out; |
|
1046
|
|
|
} |
|
1047
|
|
|
|
|
1048
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
1049
|
|
|
|
|
1050
|
|
|
// Added by James Heinrich <[email protected]> - January 5, 2003 |
|
1051
|
|
|
|
|
1052
|
|
|
// Takes raw image data and plots it pixel-by-pixel on a new GD image and returns that |
|
1053
|
|
|
// It's extremely slow, but the only solution when imagecreatefromstring() fails |
|
1054
|
|
|
public function getGD_PixelPlotterVersion() |
|
1055
|
|
|
{ |
|
1056
|
|
|
if (!$this->m_bLoaded) { |
|
1057
|
|
|
return false; |
|
1058
|
|
|
} |
|
1059
|
|
|
|
|
1060
|
|
|
// PREPARE COLOR TABLE (RGBQUADs) |
|
1061
|
|
|
if ($this->m_img->m_gih->m_bLocalClr) { |
|
1062
|
|
|
$pal = $this->m_img->m_gih->m_colorTable->toString(); |
|
1063
|
|
|
} elseif ($this->m_gfh->m_bGlobalClr) { |
|
1064
|
|
|
$pal = $this->m_gfh->m_colorTable->toString(); |
|
1065
|
|
|
} else { |
|
1066
|
|
|
die('No color table available in getGD_PixelPlotterVersion()'); |
|
|
|
|
|
|
1067
|
|
|
} |
|
1068
|
|
|
|
|
1069
|
|
|
$PlottingIMG = imagecreate($this->m_gfh->m_nWidth, $this->m_gfh->m_nHeight); |
|
1070
|
|
|
$NumColorsInPal = floor(strlen($pal) / 3); |
|
1071
|
|
|
$ThisImageColor = []; |
|
1072
|
|
|
for ($i = 0; $i < $NumColorsInPal; $i++) { |
|
1073
|
|
|
$ThisImageColor[$i] = imagecolorallocate( |
|
1074
|
|
|
$PlottingIMG, |
|
1075
|
|
|
ord($pal{($i * 3) + 0}), |
|
1076
|
|
|
ord($pal{($i * 3) + 1}), |
|
1077
|
|
|
ord($pal{($i * 3) + 2}) |
|
1078
|
|
|
); |
|
1079
|
|
|
} |
|
1080
|
|
|
|
|
1081
|
|
|
// PREPARE BITMAP BITS |
|
1082
|
|
|
$data = $this->m_img->m_data; |
|
1083
|
|
|
$nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth; |
|
1084
|
|
|
for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { |
|
1085
|
|
|
if (!phpthumb_functions::FunctionIsDisabled('set_time_limit')) { |
|
1086
|
|
|
set_time_limit(30); |
|
1087
|
|
|
} |
|
1088
|
|
|
for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { |
|
1089
|
|
|
if (($x >= $this->m_img->m_gih->m_nLeft) |
|
1090
|
|
|
&& ($y >= $this->m_img->m_gih->m_nTop) |
|
1091
|
|
|
&& ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) |
|
1092
|
|
|
&& ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) { |
|
1093
|
|
|
// PART OF IMAGE |
|
1094
|
|
|
if (@$this->m_img->m_bTrans && (ord($data[$nPxl]) == $this->m_img->m_nTrans)) { |
|
1095
|
|
|
imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]); |
|
1096
|
|
|
} else { |
|
1097
|
|
|
imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[ord($data[$nPxl])]); |
|
1098
|
|
|
} |
|
1099
|
|
|
} else { |
|
1100
|
|
|
// BACKGROUND |
|
1101
|
|
|
imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]); |
|
1102
|
|
|
} |
|
1103
|
|
|
} |
|
1104
|
|
|
$nPxl -= $this->m_gfh->m_nWidth << 1; |
|
1105
|
|
|
} |
|
1106
|
|
|
|
|
1107
|
|
|
return $PlottingIMG; |
|
1108
|
|
|
} |
|
1109
|
|
|
|
|
1110
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
1111
|
|
|
|
|
1112
|
|
|
public function dword($val) |
|
1113
|
|
|
{ |
|
1114
|
|
|
$val = (int)$val; |
|
1115
|
|
|
return chr($val & 0xFF) . chr(($val & 0xFF00) >> 8) . chr(($val & 0xFF0000) >> 16) . chr(($val & 0xFF000000) >> 24); |
|
1116
|
|
|
} |
|
1117
|
|
|
|
|
1118
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
1119
|
|
|
|
|
1120
|
|
|
public function ndword($val) |
|
1121
|
|
|
{ |
|
1122
|
|
|
$val = (int)$val; |
|
1123
|
|
|
return chr(($val & 0xFF000000) >> 24) . chr(($val & 0xFF0000) >> 16) . chr(($val & 0xFF00) >> 8) . chr($val & 0xFF); |
|
1124
|
|
|
} |
|
1125
|
|
|
|
|
1126
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
1127
|
|
|
|
|
1128
|
|
|
public function width() |
|
1129
|
|
|
{ |
|
1130
|
|
|
return $this->m_gfh->m_nWidth; |
|
1131
|
|
|
} |
|
1132
|
|
|
|
|
1133
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
1134
|
|
|
|
|
1135
|
|
|
public function height() |
|
1136
|
|
|
{ |
|
1137
|
|
|
return $this->m_gfh->m_nHeight; |
|
1138
|
|
|
} |
|
1139
|
|
|
|
|
1140
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
1141
|
|
|
|
|
1142
|
|
|
public function comment() |
|
1143
|
|
|
{ |
|
1144
|
|
|
return $this->m_img->m_lpComm; |
|
1145
|
|
|
} |
|
1146
|
|
|
|
|
1147
|
|
|
/////////////////////////////////////////////////////////////////////////// |
|
1148
|
|
|
|
|
1149
|
|
|
public function loaded() |
|
1150
|
|
|
{ |
|
1151
|
|
|
return $this->m_bLoaded; |
|
1152
|
|
|
} |
|
1153
|
|
|
} |
|
1154
|
|
|
|