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
15 function gif_loadFile($lpszFileName, $iIndex = 0)
19 if(!$gif->loadFile($lpszFileName, $iIndex)) {
26 function gif_outputAsBmp($gif, $lpszFileName, $bgColor = -1)
28 if(!isSet($gif) || (@get_class($gif) <> "cgif") || !$gif->loaded() || ($lpszFileName == "")) {
32 $fd = $gif->getBmp($bgColor);
33 if(strlen($fd) <= 0) {
37 if(!($fh = @fOpen($lpszFileName, "wb"))) {
40 @fWrite($fh, $fd, strlen($fd));
46 function gif_outputAsPng($gif, $lpszFileName, $bgColor = -1)
48 if(!isSet($gif) || (@get_class($gif) <> "cgif") || !$gif->loaded() || ($lpszFileName == "")) {
52 $fd = $gif->getPng($bgColor);
53 if(strlen($fd) <= 0) {
57 if(!($fh = @fOpen($lpszFileName, "wb"))) {
60 @fWrite($fh, $fd, strlen($fd));
66 function gif_outputAsJpeg($gif, $lpszFileName, $bgColor = -1)
68 if(gif_outputAsBmp($gif, "$lpszFileName.bmp", $gbColor)) {
69 exec("cjpeg $lpszFileName.bmp >$lpszFileName 2>/dev/null");
70 @unLink("$lpszFileName.bmp");
72 if(@file_exists($lpszFileName)) {
73 if(@fileSize($lpszFileName) > 0) {
77 @unLink($lpszFileName);
84 function gif_getSize($gif, &$width, &$height)
86 if(isSet($gif) && (@get_class($gif) == "cgif") && $gif->loaded()) {
87 $width = $gif->width();
88 $height = $gif->height();
90 else if(@file_exists($gif)) {
92 if(!$myGIF->getSize($gif, $width, $height)) {
106 var $Fresh, $CodeSize, $SetCodeSize, $MaxCode, $MaxCodeSize, $FirstCode, $OldCode;
107 var $ClearCode, $EndCode, $Next, $Vals, $Stack, $sp, $Buf, $CurBit, $LastBit, $Done, $LastByte;
111 $this->MAX_LZW_BITS = 12;
117 $this->Next = range(0, (1 << $this->MAX_LZW_BITS) - 1);
118 $this->Vals = range(0, (1 << $this->MAX_LZW_BITS) - 1);
119 $this->Stack = range(0, (1 << ($this->MAX_LZW_BITS + 1)) - 1);
120 $this->Buf = range(0, 279);
123 function deCompress($data, &$datLen) {
124 $stLen = strlen($data);
129 $this->LZWCommand($data, true);
131 while(($iIndex = $this->LZWCommand($data, false)) >= 0) {
132 $ret .= chr($iIndex);
135 $datLen = $stLen - strlen($data);
144 function LZWCommand(&$data, $bInit) {
146 $this->SetCodeSize = ord($data{0});
147 $data = substr($data, 1);
149 $this->CodeSize = $this->SetCodeSize + 1;
150 $this->ClearCode = 1 << $this->SetCodeSize;
151 $this->EndCode = $this->ClearCode + 1;
152 $this->MaxCode = $this->ClearCode + 2;
153 $this->MaxCodeSize = $this->ClearCode << 1;
155 $this->GetCode($data, $bInit);
158 for($i = 0; $i < $this->ClearCode; $i++) {
160 $this->Vals[$i] = $i;
163 for(; $i < (1 << $this->MAX_LZW_BITS); $i++) {
175 $this->FirstCode = $this->GetCode($data, $bInit);
176 $this->OldCode = $this->FirstCode;
178 while($this->FirstCode == $this->ClearCode);
180 return $this->FirstCode;
185 return $this->Stack[$this->sp];
188 while(($Code = $this->GetCode($data, $bInit)) >= 0) {
189 if($Code == $this->ClearCode) {
190 for($i = 0; $i < $this->ClearCode; $i++) {
192 $this->Vals[$i] = $i;
195 for(; $i < (1 << $this->MAX_LZW_BITS); $i++) {
200 $this->CodeSize = $this->SetCodeSize + 1;
201 $this->MaxCodeSize = $this->ClearCode << 1;
202 $this->MaxCode = $this->ClearCode + 2;
204 $this->FirstCode = $this->GetCode($data, $bInit);
205 $this->OldCode = $this->FirstCode;
207 return $this->FirstCode;
210 if($Code == $this->EndCode) {
215 if($Code >= $this->MaxCode) {
216 $this->Stack[$this->sp] = $this->FirstCode;
218 $Code = $this->OldCode;
221 while($Code >= $this->ClearCode) {
222 $this->Stack[$this->sp] = $this->Vals[$Code];
225 if($Code == $this->Next[$Code]) // Circular table entry, big GIF Error!
228 $Code = $this->Next[$Code];
231 $this->FirstCode = $this->Vals[$Code];
232 $this->Stack[$this->sp] = $this->FirstCode;
235 if(($Code = $this->MaxCode) < (1 << $this->MAX_LZW_BITS)) {
236 $this->Next[$Code] = $this->OldCode;
237 $this->Vals[$Code] = $this->FirstCode;
240 if(($this->MaxCode >= $this->MaxCodeSize) && ($this->MaxCodeSize < (1 << $this->MAX_LZW_BITS))) {
241 $this->MaxCodeSize *= 2;
246 $this->OldCode = $InCode;
249 return $this->Stack[$this->sp];
256 function GetCode(&$data, $bInit) {
265 if(($this->CurBit + $this->CodeSize) >= $this->LastBit) {
267 if($this->CurBit >= $this->LastBit) {
268 // Ran off the end of my bits
274 $this->Buf[0] = $this->Buf[$this->LastByte - 2];
275 $this->Buf[1] = $this->Buf[$this->LastByte - 1];
277 $Count = ord($data{0});
278 $data = substr($data, 1);
281 for($i = 0; $i < $Count; $i++) {
282 $this->Buf[2 + $i] = ord($data{$i});
284 $data = substr($data, $Count);
290 $this->LastByte = 2 + $Count;
291 $this->CurBit = ($this->CurBit - $this->LastBit) + 16;
292 $this->LastBit = (2 + $Count) << 3;
296 for($i = $this->CurBit, $j = 0; $j < $this->CodeSize; $i++, $j++) {
297 $iRet |= (($this->Buf[intval($i / 8)] & (1 << ($i % 8))) != 0) << $j;
300 $this->CurBit += $this->CodeSize;
311 function CGIFCOLORTABLE() {
312 unSet($this->m_nColors);
313 unSet($this->m_arColors);
316 function load($lpData, $num) {
317 $this->m_nColors = 0;
318 $this->m_arColors = array();
320 for($i = 0; $i < $num; $i++) {
321 $rgb = substr($lpData, $i * 3, 3);
322 if(strlen($rgb) < 3) {
326 $this->m_arColors[] = (ord($rgb{2}) << 16) + (ord($rgb{1}) << 8) + ord($rgb{0});
333 function toString() {
336 for($i = 0; $i < $this->m_nColors; $i++) {
338 chr(($this->m_arColors[$i] & 0x000000FF)) . // R
339 chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G
340 chr(($this->m_arColors[$i] & 0x00FF0000) >> 16); // B
346 function toRGBQuad() {
349 for($i = 0; $i < $this->m_nColors; $i++) {
351 chr(($this->m_arColors[$i] & 0x00FF0000) >> 16) . // B
352 chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G
353 chr(($this->m_arColors[$i] & 0x000000FF)) . // R
360 function colorIndex($rgb) {
361 $rgb = intval($rgb) & 0xFFFFFF;
362 $r1 = ($rgb & 0x0000FF);
363 $g1 = ($rgb & 0x00FF00) >> 8;
364 $b1 = ($rgb & 0xFF0000) >> 16;
367 for($i = 0; $i < $this->m_nColors; $i++) {
368 $r2 = ($this->m_arColors[$i] & 0x000000FF);
369 $g2 = ($this->m_arColors[$i] & 0x0000FF00) >> 8;
370 $b2 = ($this->m_arColors[$i] & 0x00FF0000) >> 16;
371 $d = abs($r2 - $r1) + abs($g2 - $g1) + abs($b2 - $b1);
373 if(($idx == -1) || ($d < $dif)) {
397 function CGIFFILEHEADER() {
398 unSet($this->m_lpVer);
399 unSet($this->m_nWidth);
400 unSet($this->m_nHeight);
401 unSet($this->m_bGlobalClr);
402 unSet($this->m_nColorRes);
403 unSet($this->m_bSorted);
404 unSet($this->m_nTableSize);
405 unSet($this->m_nBgColor);
406 unSet($this->m_nPixelRatio);
407 unSet($this->m_colorTable);
410 function load($lpData, &$hdrLen) {
413 $this->m_lpVer = substr($lpData, 0, 6);
414 if(($this->m_lpVer <> "GIF87a") && ($this->m_lpVer <> "GIF89a")) {
418 $this->m_nWidth = $this->w2i(substr($lpData, 6, 2));
419 $this->m_nHeight = $this->w2i(substr($lpData, 8, 2));
420 if(!$this->m_nWidth || !$this->m_nHeight) {
424 $b = ord(substr($lpData, 10, 1));
425 $this->m_bGlobalClr = ($b & 0x80) ? true : false;
426 $this->m_nColorRes = ($b & 0x70) >> 4;
427 $this->m_bSorted = ($b & 0x08) ? true : false;
428 $this->m_nTableSize = 2 << ($b & 0x07);
429 $this->m_nBgColor = ord(substr($lpData, 11, 1));
430 $this->m_nPixelRatio = ord(substr($lpData, 12, 1));
433 if($this->m_bGlobalClr) {
434 $this->m_colorTable = new CGIFCOLORTABLE();
435 if(!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
438 $hdrLen += 3 * $this->m_nTableSize;
445 return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
449 class CGIFIMAGEHEADER
462 function CGIFIMAGEHEADER() {
463 unSet($this->m_nLeft);
464 unSet($this->m_nTop);
465 unSet($this->m_nWidth);
466 unSet($this->m_nHeight);
467 unSet($this->m_bLocalClr);
468 unSet($this->m_bInterlace);
469 unSet($this->m_bSorted);
470 unSet($this->m_nTableSize);
471 unSet($this->m_colorTable);
474 function load($lpData, &$hdrLen) {
477 $this->m_nLeft = $this->w2i(substr($lpData, 0, 2));
478 $this->m_nTop = $this->w2i(substr($lpData, 2, 2));
479 $this->m_nWidth = $this->w2i(substr($lpData, 4, 2));
480 $this->m_nHeight = $this->w2i(substr($lpData, 6, 2));
482 if(!$this->m_nWidth || !$this->m_nHeight) {
486 $b = ord($lpData{8});
487 $this->m_bLocalClr = ($b & 0x80) ? true : false;
488 $this->m_bInterlace = ($b & 0x40) ? true : false;
489 $this->m_bSorted = ($b & 0x20) ? true : false;
490 $this->m_nTableSize = 2 << ($b & 0x07);
493 if($this->m_bLocalClr) {
494 $this->m_colorTable = new CGIFCOLORTABLE();
495 if(!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
498 $hdrLen += 3 * $this->m_nTableSize;
505 return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
521 function CGIFIMAGE() {
522 unSet($this->m_disp);
523 unSet($this->m_bUser);
524 unSet($this->m_bTrans);
525 unSet($this->m_nDelay);
526 unSet($this->m_nTrans);
527 unSet($this->m_lpComm);
528 unSet($this->m_data);
529 $this->m_gih = new CGIFIMAGEHEADER();
530 $this->m_lzw = new CGIFLZW();
533 function load($data, &$datLen) {
538 $data = substr($data, 1);
542 case 0x21: // Extension
543 if(!$this->skipExt($data, $len = 0)) {
550 // LOAD HEADER & COLOR TABLE
551 if(!$this->m_gih->load($data, $len = 0)) {
554 $data = substr($data, $len);
558 if(!($this->m_data = $this->m_lzw->deCompress($data, $len = 0))) {
561 $data = substr($data, $len);
564 if($this->m_gih->m_bInterlace) {
565 $this->deInterlace();
577 function skipExt(&$data, &$extLen) {
581 $data = substr($data, 1);
585 case 0xF9: // Graphic Control
587 $this->m_disp = ($b & 0x1C) >> 2;
588 $this->m_bUser = ($b & 0x02) ? true : false;
589 $this->m_bTrans = ($b & 0x01) ? true : false;
590 $this->m_nDelay = $this->w2i(substr($data, 2, 2));
591 $this->m_nTrans = ord($data{4});
594 case 0xFE: // Comment
595 $this->m_lpComm = substr($data, 1, ord($data{0}));
598 case 0x01: // Plain text
601 case 0xFF: // Application
605 // SKIP DEFAULT AS DEFS MAY CHANGE
607 $data = substr($data, 1);
610 $data = substr($data, $b);
613 $data = substr($data, 1);
620 return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
623 function deInterlace() {
624 $data = $this->m_data;
626 for($i = 0; $i < 4; $i++) {
649 for(; $y < $this->m_gih->m_nHeight; $y += $s) {
650 $lne = substr($this->m_data, 0, $this->m_gih->m_nWidth);
651 $this->m_data = substr($this->m_data, $this->m_gih->m_nWidth);
654 substr($data, 0, $y * $this->m_gih->m_nWidth) .
656 substr($data, ($y + 1) * $this->m_gih->m_nWidth);
660 $this->m_data = $data;
673 $this->m_gfh = new CGIFFILEHEADER();
674 $this->m_img = new CGIFIMAGE();
675 $this->m_lpData = "";
676 $this->m_bLoaded = false;
679 function loadFile($lpszFileName, $iIndex) {
685 if(!($fh = @fOpen($lpszFileName, "rb"))) {
688 $data = @fRead($fh, @fileSize($lpszFileName));
690 // $data=fread($fh,filesize($lpszFileName));
692 $data = $data . @fread($fh, 1024);
694 $this->m_lpData = @fRead($fh, @fileSize($lpszFileName));
698 if(!$this->m_gfh->load($this->m_lpData, $len = 0)) {
701 $this->m_lpData = substr($this->m_lpData, $len);
704 if(!$this->m_img->load($this->m_lpData, $imgLen = 0)) {
707 $this->m_lpData = substr($this->m_lpData, $imgLen);
709 while($iIndex-- > 0);
711 $this->m_bLoaded = true;
715 function getSize($lpszFileName, &$width, &$height)
717 if(!($fh = @fOpen($lpszFileName, "rb"))) {
720 $data = @fRead($fh, @fileSize($lpszFileName));
723 $gfh = new CGIFFILEHEADER();
724 if(!$gfh->load($data, $len = 0)) {
728 $width = $gfh->m_nWidth;
729 $height = $gfh->m_nHeight;
733 function getBmp($bgColor) {
736 if(!$this->m_bLoaded) {
740 // PREPARE COLOR TABLE (RGBQUADs)
741 if($this->m_img->m_gih->m_bLocalClr) {
742 $nColors = $this->m_img->m_gih->m_nTableSize;
743 $rgbq = $this->m_img->m_gih->m_colorTable->toRGBQuad();
745 $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
748 else if($this->m_gfh->m_bGlobalClr) {
749 $nColors = $this->m_gfh->m_nTableSize;
750 $rgbq = $this->m_gfh->m_colorTable->toRGBQuad();
752 $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
760 // PREPARE BITMAP BITS
761 $data = $this->m_img->m_data;
762 $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth;
765 $nPad = ($this->m_gfh->m_nWidth % 4) ? 4 - ($this->m_gfh->m_nWidth % 4) : 0;
766 for($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
767 for($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
769 ($x >= $this->m_img->m_gih->m_nLeft) &&
770 ($y >= $this->m_img->m_gih->m_nTop) &&
771 ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) &&
772 ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) {
774 if($this->m_img->m_bTrans && (ord($data{$nPxl}) == $this->m_img->m_nTrans)) {
775 // TRANSPARENT -> BACKGROUND
777 $bmp .= chr($this->m_gfh->m_nBgColor);
780 $bmp .= chr($bgColor);
784 $bmp .= $data{$nPxl};
790 $bmp .= chr($this->m_gfh->m_nBgColor);
793 $bmp .= chr($bgColor);
797 $nPxl -= $this->m_gfh->m_nWidth << 1;
800 for($x = 0; $x < $nPad; $x++) {
807 $out .= $this->dword(14 + 40 + ($nColors << 2) + strlen($bmp));
810 $out .= $this->dword(14 + 40 + ($nColors << 2));
813 $out .= $this->dword(40);
814 $out .= $this->dword($this->m_gfh->m_nWidth);
815 $out .= $this->dword($this->m_gfh->m_nHeight);
818 $out .= "\x00\x00\x00\x00";
819 $out .= "\x00\x00\x00\x00";
820 $out .= "\x12\x0B\x00\x00";
821 $out .= "\x12\x0B\x00\x00";
822 $out .= $this->dword($nColors % 256);
823 $out .= "\x00\x00\x00\x00";
836 function getPng($bgColor) {
839 if(!$this->m_bLoaded) {
843 // PREPARE COLOR TABLE (RGBQUADs)
844 if($this->m_img->m_gih->m_bLocalClr) {
845 $nColors = $this->m_img->m_gih->m_nTableSize;
846 $pal = $this->m_img->m_gih->m_colorTable->toString();
848 $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
851 else if($this->m_gfh->m_bGlobalClr) {
852 $nColors = $this->m_gfh->m_nTableSize;
853 $pal = $this->m_gfh->m_colorTable->toString();
855 $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
863 // PREPARE BITMAP BITS
864 $data = $this->m_img->m_data;
867 for($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
869 for($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
871 ($x >= $this->m_img->m_gih->m_nLeft) &&
872 ($y >= $this->m_img->m_gih->m_nTop) &&
873 ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) &&
874 ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) {
876 $bmp .= $data{$nPxl};
881 $bmp .= chr($this->m_gfh->m_nBgColor);
884 $bmp .= chr($bgColor);
889 $bmp = gzcompress($bmp, 9);
892 $out .= "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
895 $out .= "\x00\x00\x00\x0D";
897 $tmp .= $this->ndword($this->m_gfh->m_nWidth);
898 $tmp .= $this->ndword($this->m_gfh->m_nHeight);
899 $tmp .= "\x08\x03\x00\x00\x00";
901 $out .= $this->ndword(crc32($tmp));
905 $out .= $this->ndword($nColors * 3);
909 $out .= $this->ndword(crc32($tmp));
913 if($this->m_img->m_bTrans && ($nColors > 0)) {
914 $out .= $this->ndword($nColors);
916 for($i = 0; $i < $nColors; $i++) {
917 $tmp .= ($i == $this->m_img->m_nTrans) ? "\x00" : "\xFF";
920 $out .= $this->ndword(crc32($tmp));
924 $out .= $this->ndword(strlen($bmp));
928 $out .= $this->ndword(crc32($tmp));
931 $out .= "\x00\x00\x00\x00IEND\xAE\x42\x60\x82";
936 function dword($val) {
938 return chr($val & 0xFF).chr(($val & 0xFF00) >> 8).chr(($val & 0xFF0000) >> 16).chr(($val & 0xFF000000) >> 24);
941 function ndword($val) {
943 return chr(($val & 0xFF000000) >> 24).chr(($val & 0xFF0000) >> 16).chr(($val & 0xFF00) >> 8).chr($val & 0xFF);
947 return $this->m_gfh->m_nWidth;
951 return $this->m_gfh->m_nHeight;
955 return $this->m_img->m_lpComm;
959 return $this->m_bLoaded;
967 // c-hanging-comment-ender-p: nil
968 // indent-tabs-mode: nil