source: trunk/spip/esqueleto-redcta/plugins/article_pdf/pdf/gif.php @ 30

Last change on this file since 30 was 30, checked in by sebas, 17 years ago

nueva importacion del codigo del esqueleto de redcta con los plugins

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