3 // GIF Util - (C) 2003 Yamasoft (S/C)
4 // http://www.yamasoft.com
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.
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
17 function gif_loadFile($lpszFileName, $iIndex = 0)
21 if(!$gif->loadFile($lpszFileName, $iIndex)) {
30 function gif_outputAsBmp($gif, $lpszFileName, $bgColor = -1)
32 if(!isSet($gif) || (@get_class($gif) <> "cgif") || !$gif->loaded() || ($lpszFileName == "")) {
36 $fd = $gif->getBmp($bgColor);
37 if(strlen($fd) <= 0) {
41 if(!($fh = @fOpen($lpszFileName, "wb"))) {
44 @fWrite($fh, $fd, strlen($fd));
52 function gif_outputAsPng($gif, $lpszFileName, $bgColor = -1)
54 if(!isSet($gif) || (@get_class($gif) <> "cgif") || !$gif->loaded() || ($lpszFileName == "")) {
58 $fd = $gif->getPng($bgColor);
59 if(strlen($fd) <= 0) {
63 if(!($fh = @fOpen($lpszFileName, "wb"))) {
66 @fWrite($fh, $fd, strlen($fd));
74 function gif_outputAsJpeg($gif, $lpszFileName, $bgColor = -1)
76 if(gif_outputAsBmp($gif, "$lpszFileName.bmp", $gbColor)) {
77 exec("cjpeg $lpszFileName.bmp >$lpszFileName 2>/dev/null");
78 @unLink("$lpszFileName.bmp");
80 if(@file_exists($lpszFileName)) {
81 if(@fileSize($lpszFileName) > 0) {
85 @unLink($lpszFileName);
94 function gif_getSize($gif, &$width, &$height)
96 if(isSet($gif) && (@get_class($gif) == "cgif") && $gif->loaded()) {
97 $width = $gif->width();
98 $height = $gif->height();
100 else if(@file_exists($gif)) {
102 if(!$myGIF->getSize($gif, $width, $height)) {
118 var $Fresh, $CodeSize, $SetCodeSize, $MaxCode, $MaxCodeSize, $FirstCode, $OldCode;
119 var $ClearCode, $EndCode, $Next, $Vals, $Stack, $sp, $Buf, $CurBit, $LastBit, $Done, $LastByte;
123 $this->MAX_LZW_BITS = 12;
129 $this->Next = range(0, (1 << $this->MAX_LZW_BITS) - 1);
130 $this->Vals = range(0, (1 << $this->MAX_LZW_BITS) - 1);
131 $this->Stack = range(0, (1 << ($this->MAX_LZW_BITS + 1)) - 1);
132 $this->Buf = range(0, 279);
135 function deCompress($data, &$datLen) {
136 $stLen = strlen($data);
141 $this->LZWCommand($data, true);
143 while(($iIndex = $this->LZWCommand($data, false)) >= 0) {
144 $ret .= chr($iIndex);
147 $datLen = $stLen - strlen($data);
156 function LZWCommand(&$data, $bInit) {
158 $this->SetCodeSize = ord($data{0});
159 $data = substr($data, 1);
161 $this->CodeSize = $this->SetCodeSize + 1;
162 $this->ClearCode = 1 << $this->SetCodeSize;
163 $this->EndCode = $this->ClearCode + 1;
164 $this->MaxCode = $this->ClearCode + 2;
165 $this->MaxCodeSize = $this->ClearCode << 1;
167 $this->GetCode($data, $bInit);
170 for($i = 0; $i < $this->ClearCode; $i++) {
172 $this->Vals[$i] = $i;
175 for(; $i < (1 << $this->MAX_LZW_BITS); $i++) {
187 $this->FirstCode = $this->GetCode($data, $bInit);
188 $this->OldCode = $this->FirstCode;
190 while($this->FirstCode == $this->ClearCode);
192 return $this->FirstCode;
197 return $this->Stack[$this->sp];
200 while(($Code = $this->GetCode($data, $bInit)) >= 0) {
201 if($Code == $this->ClearCode) {
202 for($i = 0; $i < $this->ClearCode; $i++) {
204 $this->Vals[$i] = $i;
207 for(; $i < (1 << $this->MAX_LZW_BITS); $i++) {
212 $this->CodeSize = $this->SetCodeSize + 1;
213 $this->MaxCodeSize = $this->ClearCode << 1;
214 $this->MaxCode = $this->ClearCode + 2;
216 $this->FirstCode = $this->GetCode($data, $bInit);
217 $this->OldCode = $this->FirstCode;
219 return $this->FirstCode;
222 if($Code == $this->EndCode) {
227 if($Code >= $this->MaxCode) {
228 $this->Stack[$this->sp] = $this->FirstCode;
230 $Code = $this->OldCode;
233 while($Code >= $this->ClearCode) {
234 $this->Stack[$this->sp] = $this->Vals[$Code];
237 if($Code == $this->Next[$Code]) // Circular table entry, big GIF Error!
240 $Code = $this->Next[$Code];
243 $this->FirstCode = $this->Vals[$Code];
244 $this->Stack[$this->sp] = $this->FirstCode;
247 if(($Code = $this->MaxCode) < (1 << $this->MAX_LZW_BITS)) {
248 $this->Next[$Code] = $this->OldCode;
249 $this->Vals[$Code] = $this->FirstCode;
252 if(($this->MaxCode >= $this->MaxCodeSize) && ($this->MaxCodeSize < (1 << $this->MAX_LZW_BITS))) {
253 $this->MaxCodeSize *= 2;
258 $this->OldCode = $InCode;
261 return $this->Stack[$this->sp];
268 function GetCode(&$data, $bInit) {
277 if(($this->CurBit + $this->CodeSize) >= $this->LastBit) {
279 if($this->CurBit >= $this->LastBit) {
280 // Ran off the end of my bits
286 $this->Buf[0] = $this->Buf[$this->LastByte - 2];
287 $this->Buf[1] = $this->Buf[$this->LastByte - 1];
289 $Count = ord($data{0});
290 $data = substr($data, 1);
293 for($i = 0; $i < $Count; $i++) {
294 $this->Buf[2 + $i] = ord($data{$i});
296 $data = substr($data, $Count);
302 $this->LastByte = 2 + $Count;
303 $this->CurBit = ($this->CurBit - $this->LastBit) + 16;
304 $this->LastBit = (2 + $Count) << 3;
308 for($i = $this->CurBit, $j = 0; $j < $this->CodeSize; $i++, $j++) {
309 $iRet |= (($this->Buf[intval($i / 8)] & (1 << ($i % 8))) != 0) << $j;
312 $this->CurBit += $this->CodeSize;
323 function CGIFCOLORTABLE() {
324 unSet($this->m_nColors);
325 unSet($this->m_arColors);
329 function load($lpData, $num) {
330 $this->m_nColors = 0;
331 $this->m_arColors = array();
333 for($i = 0; $i < $num; $i++) {
334 $rgb = substr($lpData, $i * 3, 3);
335 if(strlen($rgb) < 3) {
339 $this->m_arColors[] = (ord($rgb{2}) << 16) + (ord($rgb{1}) << 8) + ord($rgb{0});
346 function toString() {
349 for($i = 0; $i < $this->m_nColors; $i++) {
351 chr(($this->m_arColors[$i] & 0x000000FF)) . // R
352 chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G
353 chr(($this->m_arColors[$i] & 0x00FF0000) >> 16); // B
359 function toRGBQuad() {
362 for($i = 0; $i < $this->m_nColors; $i++) {
364 chr(($this->m_arColors[$i] & 0x00FF0000) >> 16) . // B
365 chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G
366 chr(($this->m_arColors[$i] & 0x000000FF)) . // R
373 function colorIndex($rgb) {
374 $rgb = intval($rgb) & 0xFFFFFF;
375 $r1 = ($rgb & 0x0000FF);
376 $g1 = ($rgb & 0x00FF00) >> 8;
377 $b1 = ($rgb & 0xFF0000) >> 16;
380 for($i = 0; $i < $this->m_nColors; $i++) {
381 $r2 = ($this->m_arColors[$i] & 0x000000FF);
382 $g2 = ($this->m_arColors[$i] & 0x0000FF00) >> 8;
383 $b2 = ($this->m_arColors[$i] & 0x00FF0000) >> 16;
384 $d = abs($r2 - $r1) + abs($g2 - $g1) + abs($b2 - $b1);
386 if(($idx == -1) || ($d < $dif)) {
410 function CGIFFILEHEADER() {
411 unSet($this->m_lpVer);
412 unSet($this->m_nWidth);
413 unSet($this->m_nHeight);
414 unSet($this->m_bGlobalClr);
415 unSet($this->m_nColorRes);
416 unSet($this->m_bSorted);
417 unSet($this->m_nTableSize);
418 unSet($this->m_nBgColor);
419 unSet($this->m_nPixelRatio);
420 unSet($this->m_colorTable);
424 function load($lpData, &$hdrLen) {
427 $this->m_lpVer = substr($lpData, 0, 6);
428 if(($this->m_lpVer <> "GIF87a") && ($this->m_lpVer <> "GIF89a")) {
432 $this->m_nWidth = $this->w2i(substr($lpData, 6, 2));
433 $this->m_nHeight = $this->w2i(substr($lpData, 8, 2));
434 if(!$this->m_nWidth || !$this->m_nHeight) {
438 $b = ord(substr($lpData, 10, 1));
439 $this->m_bGlobalClr = ($b & 0x80) ? true : false;
440 $this->m_nColorRes = ($b & 0x70) >> 4;
441 $this->m_bSorted = ($b & 0x08) ? true : false;
442 $this->m_nTableSize = 2 << ($b & 0x07);
443 $this->m_nBgColor = ord(substr($lpData, 11, 1));
444 $this->m_nPixelRatio = ord(substr($lpData, 12, 1));
447 if($this->m_bGlobalClr) {
448 $this->m_colorTable = new CGIFCOLORTABLE();
449 if(!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
452 $hdrLen += 3 * $this->m_nTableSize;
459 return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
463 class CGIFIMAGEHEADER
476 function CGIFIMAGEHEADER() {
477 unSet($this->m_nLeft);
478 unSet($this->m_nTop);
479 unSet($this->m_nWidth);
480 unSet($this->m_nHeight);
481 unSet($this->m_bLocalClr);
482 unSet($this->m_bInterlace);
483 unSet($this->m_bSorted);
484 unSet($this->m_nTableSize);
485 unSet($this->m_colorTable);
488 function load($lpData, &$hdrLen) {
491 $this->m_nLeft = $this->w2i(substr($lpData, 0, 2));
492 $this->m_nTop = $this->w2i(substr($lpData, 2, 2));
493 $this->m_nWidth = $this->w2i(substr($lpData, 4, 2));
494 $this->m_nHeight = $this->w2i(substr($lpData, 6, 2));
496 if(!$this->m_nWidth || !$this->m_nHeight) {
500 $b = ord($lpData{8});
501 $this->m_bLocalClr = ($b & 0x80) ? true : false;
502 $this->m_bInterlace = ($b & 0x40) ? true : false;
503 $this->m_bSorted = ($b & 0x20) ? true : false;
504 $this->m_nTableSize = 2 << ($b & 0x07);
507 if($this->m_bLocalClr) {
508 $this->m_colorTable = new CGIFCOLORTABLE();
509 if(!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
512 $hdrLen += 3 * $this->m_nTableSize;
519 return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
535 function CGIFIMAGE() {
536 unSet($this->m_disp);
537 unSet($this->m_bUser);
538 unSet($this->m_bTrans);
539 unSet($this->m_nDelay);
540 unSet($this->m_nTrans);
541 unSet($this->m_lpComm);
542 unSet($this->m_data);
543 $this->m_gih = new CGIFIMAGEHEADER();
544 $this->m_lzw = new CGIFLZW();
547 function load($data, &$datLen) {
552 $data = substr($data, 1);
556 case 0x21: // Extension
557 if(!$this->skipExt($data, $len = 0)) {
564 // LOAD HEADER & COLOR TABLE
565 if(!$this->m_gih->load($data, $len = 0)) {
568 $data = substr($data, $len);
572 if(!($this->m_data = $this->m_lzw->deCompress($data, $len = 0))) {
575 $data = substr($data, $len);
578 if($this->m_gih->m_bInterlace) {
579 $this->deInterlace();
591 function skipExt(&$data, &$extLen) {
595 $data = substr($data, 1);
599 case 0xF9: // Graphic Control
601 $this->m_disp = ($b & 0x1C) >> 2;
602 $this->m_bUser = ($b & 0x02) ? true : false;
603 $this->m_bTrans = ($b & 0x01) ? true : false;
604 $this->m_nDelay = $this->w2i(substr($data, 2, 2));
605 $this->m_nTrans = ord($data{4});
608 case 0xFE: // Comment
609 $this->m_lpComm = substr($data, 1, ord($data{0}));
612 case 0x01: // Plain text
615 case 0xFF: // Application
619 // SKIP DEFAULT AS DEFS MAY CHANGE
621 $data = substr($data, 1);
624 $data = substr($data, $b);
627 $data = substr($data, 1);
634 return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
637 function deInterlace() {
638 $data = $this->m_data;
640 for($i = 0; $i < 4; $i++) {
663 for(; $y < $this->m_gih->m_nHeight; $y += $s) {
664 $lne = substr($this->m_data, 0, $this->m_gih->m_nWidth);
665 $this->m_data = substr($this->m_data, $this->m_gih->m_nWidth);
668 substr($data, 0, $y * $this->m_gih->m_nWidth) .
670 substr($data, ($y + 1) * $this->m_gih->m_nWidth);
674 $this->m_data = $data;
687 $this->m_gfh = new CGIFFILEHEADER();
688 $this->m_img = new CGIFIMAGE();
689 $this->m_lpData = "";
690 $this->m_bLoaded = false;
693 function loadFile($lpszFileName, $iIndex) {
700 if(!($fh = @fOpen($lpszFileName, "rb"))) {
703 $data = @fRead($fh, @fileSize($lpszFileName));
705 // $data=fread($fh,filesize($lpszFileName));
707 $data = $data . @fread($fh, 1024);
709 $this->m_lpData = @fRead($fh, @fileSize($lpszFileName));
713 if(!$this->m_gfh->load($this->m_lpData, $len = 0)) {
716 $this->m_lpData = substr($this->m_lpData, $len);
719 if(!$this->m_img->load($this->m_lpData, $imgLen = 0)) {
722 $this->m_lpData = substr($this->m_lpData, $imgLen);
724 while($iIndex-- > 0);
726 $this->m_bLoaded = true;
730 function getSize($lpszFileName, &$width, &$height)
732 if(!($fh = @fOpen($lpszFileName, "rb"))) {
735 $data = @fRead($fh, @fileSize($lpszFileName));
738 $gfh = new CGIFFILEHEADER();
739 if(!$gfh->load($data, $len = 0)) {
743 $width = $gfh->m_nWidth;
744 $height = $gfh->m_nHeight;
750 function getBmp($bgColor) {
753 if(!$this->m_bLoaded) {
757 // PREPARE COLOR TABLE (RGBQUADs)
758 if($this->m_img->m_gih->m_bLocalClr) {
759 $nColors = $this->m_img->m_gih->m_nTableSize;
760 $rgbq = $this->m_img->m_gih->m_colorTable->toRGBQuad();
762 $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
765 else if($this->m_gfh->m_bGlobalClr) {
766 $nColors = $this->m_gfh->m_nTableSize;
767 $rgbq = $this->m_gfh->m_colorTable->toRGBQuad();
769 $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
777 // PREPARE BITMAP BITS
778 $data = $this->m_img->m_data;
779 $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth;
782 $nPad = ($this->m_gfh->m_nWidth % 4) ? 4 - ($this->m_gfh->m_nWidth % 4) : 0;
783 for($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
784 for($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
786 ($x >= $this->m_img->m_gih->m_nLeft) &&
787 ($y >= $this->m_img->m_gih->m_nTop) &&
788 ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) &&
789 ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) {
791 if($this->m_img->m_bTrans && (ord($data{$nPxl}) == $this->m_img->m_nTrans)) {
792 // TRANSPARENT -> BACKGROUND
794 $bmp .= chr($this->m_gfh->m_nBgColor);
797 $bmp .= chr($bgColor);
801 $bmp .= $data{$nPxl};
807 $bmp .= chr($this->m_gfh->m_nBgColor);
810 $bmp .= chr($bgColor);
814 $nPxl -= $this->m_gfh->m_nWidth << 1;
817 for($x = 0; $x < $nPad; $x++) {
824 $out .= $this->dword(14 + 40 + ($nColors << 2) + strlen($bmp));
827 $out .= $this->dword(14 + 40 + ($nColors << 2));
830 $out .= $this->dword(40);
831 $out .= $this->dword($this->m_gfh->m_nWidth);
832 $out .= $this->dword($this->m_gfh->m_nHeight);
835 $out .= "\x00\x00\x00\x00";
836 $out .= "\x00\x00\x00\x00";
837 $out .= "\x12\x0B\x00\x00";
838 $out .= "\x12\x0B\x00\x00";
839 $out .= $this->dword($nColors % 256);
840 $out .= "\x00\x00\x00\x00";
855 function getPng($bgColor) {
858 if(!$this->m_bLoaded) {
862 // PREPARE COLOR TABLE (RGBQUADs)
863 if($this->m_img->m_gih->m_bLocalClr) {
864 $nColors = $this->m_img->m_gih->m_nTableSize;
865 $pal = $this->m_img->m_gih->m_colorTable->toString();
867 $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
870 else if($this->m_gfh->m_bGlobalClr) {
871 $nColors = $this->m_gfh->m_nTableSize;
872 $pal = $this->m_gfh->m_colorTable->toString();
874 $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
882 // PREPARE BITMAP BITS
883 $data = $this->m_img->m_data;
886 for($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
888 for($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
890 ($x >= $this->m_img->m_gih->m_nLeft) &&
891 ($y >= $this->m_img->m_gih->m_nTop) &&
892 ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) &&
893 ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) {
895 $bmp .= $data{$nPxl};
900 $bmp .= chr($this->m_gfh->m_nBgColor);
903 $bmp .= chr($bgColor);
908 $bmp = gzcompress($bmp, 9);
912 $out .= "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
915 $out .= "\x00\x00\x00\x0D";
917 $tmp .= $this->ndword($this->m_gfh->m_nWidth);
918 $tmp .= $this->ndword($this->m_gfh->m_nHeight);
919 $tmp .= "\x08\x03\x00\x00\x00";
921 $out .= $this->ndword(crc32($tmp));
925 $out .= $this->ndword($nColors * 3);
929 $out .= $this->ndword(crc32($tmp));
933 if($this->m_img->m_bTrans && ($nColors > 0)) {
934 $out .= $this->ndword($nColors);
936 for($i = 0; $i < $nColors; $i++) {
937 $tmp .= ($i == $this->m_img->m_nTrans) ? "\x00" : "\xFF";
940 $out .= $this->ndword(crc32($tmp));
944 $out .= $this->ndword(strlen($bmp));
948 $out .= $this->ndword(crc32($tmp));
951 $out .= "\x00\x00\x00\x00IEND\xAE\x42\x60\x82";
956 function dword($val) {
958 return chr($val & 0xFF).chr(($val & 0xFF00) >> 8).chr(($val & 0xFF0000) >> 16).chr(($val & 0xFF000000) >> 24);
961 function ndword($val) {
963 return chr(($val & 0xFF000000) >> 24).chr(($val & 0xFF0000) >> 16).chr(($val & 0xFF00) >> 8).chr($val & 0xFF);
967 return $this->m_gfh->m_nWidth;
971 return $this->m_gfh->m_nHeight;
975 return $this->m_img->m_lpComm;
979 return $this->m_bLoaded;
983 // (c-file-style: "gnu")
988 // c-hanging-comment-ender-p: nil
989 // indent-tabs-mode: nil