27 if ($gif->loadFile($lpszFileName, $iIndex)) {
45 $PNGdata = $gif->getPng($bgColor);
46 if ($img = @ImageCreateFromString($PNGdata)) {
51 } elseif ($img = $gif->getGD_PixelPlotterVersion()) {
69 if (!isSet($gif) || (@get_class($gif) <>
'cgif') || !$gif->loaded() || ($lpszFileName ==
'')) {
73 $fd = $gif->getBmp($bgColor);
74 if (strlen($fd) <= 0) {
78 if (!($fh = @fopen($lpszFileName,
'wb'))) {
81 @fwrite($fh, $fd, strlen($fd));
91 if (!isSet($gif) || (@get_class($gif) <>
'cgif') || !$gif->loaded() || ($lpszFileName ==
'')) {
95 $fd = $gif->getPng($bgColor);
96 if (strlen($fd) <= 0) {
100 if (!($fh = @fopen($lpszFileName,
'wb'))) {
103 @fwrite($fh, $fd, strlen($fd));
114 if ((strtoupper(substr(PHP_OS, 0, 3)) !=
'WIN') && (file_exists(
'/usr/local/bin/cjpeg') || `which cjpeg`)) {
117 exec(
'cjpeg '.$lpszFileName.
'.bmp >'.$lpszFileName.
' 2>/dev/null');
118 @unLink($lpszFileName.
'.bmp');
120 if (@file_exists($lpszFileName)) {
121 if (@fileSize($lpszFileName) > 0) {
125 @unLink($lpszFileName);
132 if ($img = @ImageCreateFromString($gif->getPng($bgColor))) {
133 if (@ImageJPEG($img, $lpszFileName)) {
147 if (isSet($gif) && (@get_class($gif) ==
'cgif') && $gif->loaded()) {
148 $width = $gif->width();
149 $height = $gif->height();
150 } elseif (@file_exists($gif)) {
152 if (!$myGIF->getSize($gif, $width, $height)) {
168 var
$ClearCode,
$EndCode,
$Next,
$Vals,
$Stack,
$sp,
$Buf,
$CurBit,
$LastBit,
$Done,
$LastByte;
175 $this->MAX_LZW_BITS = 12;
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);
191 $stLen = strlen($data);
198 while (($iIndex = $this->
LZWCommand($data,
false)) >= 0) {
199 $ret .= chr($iIndex);
202 $datLen = $stLen - strlen($data);
216 $this->SetCodeSize = ord($data{0});
217 $data = substr($data, 1);
219 $this->CodeSize = $this->SetCodeSize + 1;
221 $this->EndCode = $this->ClearCode + 1;
222 $this->MaxCode = $this->ClearCode + 2;
223 $this->MaxCodeSize = $this->ClearCode << 1;
230 $this->Vals[
$i] =
$i;
245 $this->FirstCode = $this->
GetCode($data, $bInit);
248 while ($this->FirstCode == $this->ClearCode);
258 while (($Code = $this->
GetCode($data, $bInit)) >= 0) {
259 if ($Code == $this->ClearCode) {
262 $this->Vals[
$i] =
$i;
270 $this->CodeSize = $this->SetCodeSize + 1;
271 $this->MaxCodeSize = $this->ClearCode << 1;
272 $this->MaxCode = $this->ClearCode + 2;
274 $this->FirstCode = $this->
GetCode($data, $bInit);
280 if ($Code == $this->EndCode) {
285 if ($Code >= $this->MaxCode) {
291 while ($Code >= $this->ClearCode) {
292 $this->Stack[
$this->sp] = $this->Vals[$Code];
295 if ($Code == $this->Next[$Code])
298 $Code = $this->Next[$Code];
301 $this->FirstCode = $this->Vals[$Code];
305 if (($Code = $this->MaxCode) < (1 << $this->MAX_LZW_BITS)) {
310 if (($this->MaxCode >= $this->MaxCodeSize) && ($this->MaxCodeSize < (1 << $this->MAX_LZW_BITS))) {
311 $this->MaxCodeSize *= 2;
316 $this->OldCode = $InCode;
338 if (($this->CurBit + $this->CodeSize) >= $this->LastBit) {
340 if ($this->CurBit >= $this->LastBit) {
347 $this->Buf[0] = $this->Buf[$this->LastByte - 2];
348 $this->Buf[1] = $this->Buf[$this->LastByte - 1];
350 $Count = ord($data{0});
351 $data = substr($data, 1);
354 for (
$i = 0;
$i < $Count;
$i++) {
355 $this->Buf[2 +
$i] = ord($data{
$i});
357 $data = substr($data, $Count);
362 $this->LastByte = 2 + $Count;
364 $this->LastBit = (2 + $Count) << 3;
369 $iRet |= (($this->Buf[intval(
$i / 8)] & (1 << (
$i % 8))) != 0) <<
$j;
389 unSet($this->m_nColors);
390 unSet($this->m_arColors);
397 $this->m_nColors = 0;
398 $this->m_arColors = array();
401 $rgb = substr($lpData,
$i * 3, 3);
402 if (strlen($rgb) < 3) {
406 $this->m_arColors[] = (ord($rgb{2}) << 16) + (ord($rgb{1}) << 8) + ord($rgb{0});
421 chr(($this->m_arColors[
$i] & 0x000000FF)) .
422 chr(($this->m_arColors[
$i] & 0x0000FF00) >> 8) .
423 chr(($this->m_arColors[
$i] & 0x00FF0000) >> 16);
437 chr(($this->m_arColors[
$i] & 0x00FF0000) >> 16) .
438 chr(($this->m_arColors[
$i] & 0x0000FF00) >> 8) .
439 chr(($this->m_arColors[
$i] & 0x000000FF)) .
450 $rgb = intval($rgb) & 0xFFFFFF;
451 $r1 = ($rgb & 0x0000FF);
452 $g1 = ($rgb & 0x00FF00) >> 8;
453 $b1 = ($rgb & 0xFF0000) >> 16;
457 $r2 = ($this->m_arColors[
$i] & 0x000000FF);
458 $g2 = ($this->m_arColors[
$i] & 0x0000FF00) >> 8;
459 $b2 = ($this->m_arColors[
$i] & 0x00FF0000) >> 16;
460 $d = abs($r2 - $r1) + abs($g2 - $g1) + abs($b2 - $b1);
462 if (($idx == -1) || ($d < $dif)) {
492 unSet($this->m_lpVer);
493 unSet($this->m_nWidth);
494 unSet($this->m_nHeight);
495 unSet($this->m_bGlobalClr);
496 unSet($this->m_nColorRes);
497 unSet($this->m_bSorted);
498 unSet($this->m_nTableSize);
499 unSet($this->m_nBgColor);
500 unSet($this->m_nPixelRatio);
501 unSet($this->m_colorTable);
506 function load($lpData, &$hdrLen)
510 $this->m_lpVer = substr($lpData, 0, 6);
511 if (($this->m_lpVer <>
'GIF87a') && ($this->m_lpVer <>
'GIF89a')) {
515 $this->m_nWidth = $this->
w2i(substr($lpData, 6, 2));
516 $this->m_nHeight = $this->
w2i(substr($lpData, 8, 2));
517 if (!$this->m_nWidth || !$this->m_nHeight) {
521 $b = ord(substr($lpData, 10, 1));
522 $this->m_bGlobalClr = ($b & 0x80) ?
true :
false;
523 $this->m_nColorRes = ($b & 0x70) >> 4;
524 $this->m_bSorted = ($b & 0x08) ?
true :
false;
525 $this->m_nTableSize = 2 << ($b & 0x07);
526 $this->m_nBgColor = ord(substr($lpData, 11, 1));
527 $this->m_nPixelRatio = ord(substr($lpData, 12, 1));
530 if ($this->m_bGlobalClr) {
532 if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
545 return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
568 unSet($this->m_nLeft);
569 unSet($this->m_nTop);
570 unSet($this->m_nWidth);
571 unSet($this->m_nHeight);
572 unSet($this->m_bLocalClr);
573 unSet($this->m_bInterlace);
574 unSet($this->m_bSorted);
575 unSet($this->m_nTableSize);
576 unSet($this->m_colorTable);
581 function load($lpData, &$hdrLen)
585 $this->m_nLeft = $this->
w2i(substr($lpData, 0, 2));
586 $this->m_nTop = $this->
w2i(substr($lpData, 2, 2));
587 $this->m_nWidth = $this->
w2i(substr($lpData, 4, 2));
588 $this->m_nHeight = $this->
w2i(substr($lpData, 6, 2));
590 if (!$this->m_nWidth || !$this->m_nHeight) {
594 $b = ord($lpData{8});
595 $this->m_bLocalClr = ($b & 0x80) ?
true :
false;
596 $this->m_bInterlace = ($b & 0x40) ?
true :
false;
597 $this->m_bSorted = ($b & 0x20) ?
true :
false;
598 $this->m_nTableSize = 2 << ($b & 0x07);
601 if ($this->m_bLocalClr) {
603 if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
616 return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
638 unSet($this->m_disp);
639 unSet($this->m_bUser);
640 unSet($this->m_bTrans);
641 unSet($this->m_nDelay);
642 unSet($this->m_nTrans);
643 unSet($this->m_lpComm);
644 unSet($this->m_data);
657 $data = substr($data, 1);
662 if (!$this->
skipExt($data, $len = 0)) {
670 if (!$this->m_gih->load($data, $len = 0)) {
673 $data = substr($data, $len);
677 if (!($this->m_data = $this->m_lzw->deCompress($data, $len = 0))) {
680 $data = substr($data, $len);
683 if ($this->m_gih->m_bInterlace) {
703 $data = substr($data, 1);
709 $this->m_disp = ($b & 0x1C) >> 2;
710 $this->m_bUser = ($b & 0x02) ?
true :
false;
711 $this->m_bTrans = ($b & 0x01) ?
true :
false;
712 $this->m_nDelay = $this->
w2i(substr($data, 2, 2));
713 $this->m_nTrans = ord($data{4});
717 $this->m_lpComm = substr($data, 1, ord($data{0}));
729 $data = substr($data, 1);
732 $data = substr($data, $b);
735 $data = substr($data, 1);
745 return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
754 for (
$i = 0;
$i < 4;
$i++) {
777 for (; $y < $this->m_gih->m_nHeight; $y += $s) {
778 $lne = substr($this->m_data, 0, $this->m_gih->m_nWidth);
779 $this->m_data = substr($this->m_data, $this->m_gih->m_nWidth);
782 substr($data, 0, $y * $this->m_gih->m_nWidth) .
784 substr($data, ($y + 1) * $this->m_gih->m_nWidth);
788 $this->m_data = $data;
808 $this->m_lpData =
'';
809 $this->m_bLoaded =
false;
821 if (!($fh = @fopen($lpszFileName,
'rb'))) {
824 $this->m_lpData = @fRead($fh, @fileSize($lpszFileName));
828 if (!$this->m_gfh->load($this->m_lpData, $len = 0)) {
831 $this->m_lpData = substr($this->m_lpData, $len);
834 if (!$this->m_img->load($this->m_lpData, $imgLen = 0)) {
837 $this->m_lpData = substr($this->m_lpData, $imgLen);
839 while ($iIndex-- > 0);
841 $this->m_bLoaded =
true;
847 function getSize($lpszFileName, &$width, &$height)
849 if (!($fh = @fopen($lpszFileName,
'rb'))) {
852 $data = @fRead($fh, @fileSize($lpszFileName));
856 if (!$gfh->load($data, $len = 0)) {
860 $width = $gfh->m_nWidth;
861 $height = $gfh->m_nHeight;
871 if (!$this->m_bLoaded) {
876 if ($this->m_img->m_gih->m_bLocalClr) {
877 $nColors = $this->m_img->m_gih->m_nTableSize;
878 $rgbq = $this->m_img->m_gih->m_colorTable->toRGBQuad();
879 if ($bgColor != -1) {
880 $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
882 } elseif ($this->m_gfh->m_bGlobalClr) {
883 $nColors = $this->m_gfh->m_nTableSize;
884 $rgbq = $this->m_gfh->m_colorTable->toRGBQuad();
885 if ($bgColor != -1) {
886 $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
894 $data = $this->m_img->m_data;
895 $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth;
897 $nPad = ($this->m_gfh->m_nWidth % 4) ? 4 - ($this->m_gfh->m_nWidth % 4) : 0;
898 for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
899 for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
901 ($x >= $this->m_img->m_gih->m_nLeft) &&
902 ($y >= $this->m_img->m_gih->m_nTop) &&
903 ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) &&
904 ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) {
906 if (@$this->m_img->m_bTrans && (ord($data{$nPxl}) == $this->m_img->m_nTrans)) {
908 if ($bgColor == -1) {
909 $bmp .= chr($this->m_gfh->m_nBgColor);
911 $bmp .= chr($bgColor);
914 $bmp .= $data{$nPxl};
918 if ($bgColor == -1) {
919 $bmp .= chr($this->m_gfh->m_nBgColor);
921 $bmp .= chr($bgColor);
925 $nPxl -= $this->m_gfh->m_nWidth << 1;
928 for ($x = 0; $x < $nPad; $x++) {
935 $out .= $this->
dword(14 + 40 + ($nColors << 2) + strlen($bmp));
938 $out .= $this->
dword(14 + 40 + ($nColors << 2));
941 $out .= $this->
dword(40);
942 $out .= $this->
dword($this->m_gfh->m_nWidth);
943 $out .= $this->
dword($this->m_gfh->m_nHeight);
946 $out .=
"\x00\x00\x00\x00";
947 $out .=
"\x00\x00\x00\x00";
948 $out .=
"\x12\x0B\x00\x00";
949 $out .=
"\x12\x0B\x00\x00";
950 $out .= $this->
dword($nColors % 256);
951 $out .=
"\x00\x00\x00\x00";
970 if (!$this->m_bLoaded) {
975 if ($this->m_img->m_gih->m_bLocalClr) {
976 $nColors = $this->m_img->m_gih->m_nTableSize;
977 $pal = $this->m_img->m_gih->m_colorTable->toString();
978 if ($bgColor != -1) {
979 $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
981 } elseif ($this->m_gfh->m_bGlobalClr) {
982 $nColors = $this->m_gfh->m_nTableSize;
983 $pal = $this->m_gfh->m_colorTable->toString();
984 if ($bgColor != -1) {
985 $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
993 $data = $this->m_img->m_data;
996 for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
998 for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
1000 ($x >= $this->m_img->m_gih->m_nLeft) &&
1001 ($y >= $this->m_img->m_gih->m_nTop) &&
1002 ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) &&
1003 ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) {
1005 $bmp .= $data{$nPxl};
1008 if ($bgColor == -1) {
1009 $bmp .= chr($this->m_gfh->m_nBgColor);
1011 $bmp .= chr($bgColor);
1016 $bmp = gzcompress($bmp, 9);
1020 $out .=
"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
1023 $out .=
"\x00\x00\x00\x0D";
1025 $tmp .= $this->
ndword($this->m_gfh->m_nWidth);
1026 $tmp .= $this->
ndword($this->m_gfh->m_nHeight);
1027 $tmp .=
"\x08\x03\x00\x00\x00";
1029 $out .= $this->
ndword(crc32($tmp));
1033 $out .= $this->
ndword($nColors * 3);
1037 $out .= $this->
ndword(crc32($tmp));
1041 if (@$this->m_img->m_bTrans && ($nColors > 0)) {
1042 $out .= $this->
ndword($nColors);
1044 for (
$i = 0;
$i < $nColors;
$i++) {
1045 $tmp .= (
$i == $this->m_img->m_nTrans) ?
"\x00" :
"\xFF";
1048 $out .= $this->
ndword(crc32($tmp));
1052 $out .= $this->
ndword(strlen($bmp));
1056 $out .= $this->
ndword(crc32($tmp));
1059 $out .=
"\x00\x00\x00\x00IEND\xAE\x42\x60\x82";
1072 if (!$this->m_bLoaded) {
1077 if ($this->m_img->m_gih->m_bLocalClr) {
1078 $pal = $this->m_img->m_gih->m_colorTable->toString();
1079 } elseif ($this->m_gfh->m_bGlobalClr) {
1080 $pal = $this->m_gfh->m_colorTable->toString();
1082 die(
'No color table available in getGD_PixelPlotterVersion()');
1085 $PlottingIMG = ImageCreate($this->m_gfh->m_nWidth, $this->m_gfh->m_nHeight);
1086 $NumColorsInPal = floor(strlen($pal) / 3);
1087 for (
$i = 0;
$i < $NumColorsInPal;
$i++) {
1088 $ThisImageColor[
$i] = ImageColorAllocate(
1090 ord($pal{((
$i * 3) + 0)}),
1091 ord($pal{((
$i * 3) + 1)}),
1092 ord($pal{((
$i * 3) + 2)}));
1096 $data = $this->m_img->m_data;
1097 $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth;
1098 for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
1102 for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
1104 ($x >= $this->m_img->m_gih->m_nLeft) &&
1105 ($y >= $this->m_img->m_gih->m_nTop) &&
1106 ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) &&
1107 ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) {
1109 if (@$this->m_img->m_bTrans && (ord($data{$nPxl}) == $this->m_img->m_nTrans)) {
1110 ImageSetPixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]);
1112 ImageSetPixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[ord($data{$nPxl})]);
1116 ImageSetPixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]);
1119 $nPxl -= $this->m_gfh->m_nWidth << 1;
1123 return $PlottingIMG;
1130 $val = intval($val);
1131 return chr($val & 0xFF).chr(($val & 0xFF00) >> 8).chr(($val & 0xFF0000) >> 16).chr(($val & 0xFF000000) >> 24);
1138 $val = intval($val);
1139 return chr(($val & 0xFF000000) >> 24).chr(($val & 0xFF0000) >> 16).chr(($val & 0xFF00) >> 8).chr($val & 0xFF);
1146 return $this->m_gfh->m_nWidth;
1153 return $this->m_gfh->m_nHeight;
1160 return $this->m_img->m_lpComm;