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