📄 gif.php
字号:
return false;
}
$b = ord($lpData{8});
$this->m_bLocalClr = ($b & 0x80) ? true : false;
$this->m_bInterlace = ($b & 0x40) ? true : false;
$this->m_bSorted = ($b & 0x20) ? true : false;
$this->m_nTableSize = 2 << ($b & 0x07);
$hdrLen = 9;
if($this->m_bLocalClr) {
$this->m_colorTable = new CGIFCOLORTABLE();
if(!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
return false;
}
$hdrLen += 3 * $this->m_nTableSize;
}
return true;
}
///////////////////////////////////////////////////////////////////////////
function w2i($str)
{
return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
class CGIFIMAGE
{
var $m_disp;
var $m_bUser;
var $m_bTrans;
var $m_nDelay;
var $m_nTrans;
var $m_lpComm;
var $m_gih;
var $m_data;
var $m_lzw;
///////////////////////////////////////////////////////////////////////////
function CGIFIMAGE()
{
unSet($this->m_disp);
unSet($this->m_bUser);
unSet($this->m_bTrans);
unSet($this->m_nDelay);
unSet($this->m_nTrans);
unSet($this->m_lpComm);
unSet($this->m_data);
$this->m_gih = new CGIFIMAGEHEADER();
$this->m_lzw = new CGIFLZW();
}
///////////////////////////////////////////////////////////////////////////
function load($data, &$datLen)
{
$datLen = 0;
while(true) {
$b = ord($data{0});
$data = substr($data, 1);
$datLen++;
switch($b) {
case 0x21: // Extension
if(!$this->skipExt($data, $len = 0)) {
return false;
}
$datLen += $len;
break;
case 0x2C: // Image
// LOAD HEADER & COLOR TABLE
if(!$this->m_gih->load($data, $len = 0)) {
return false;
}
$data = substr($data, $len);
$datLen += $len;
// ALLOC BUFFER
if(!($this->m_data = $this->m_lzw->deCompress($data, $len = 0))) {
return false;
}
$data = substr($data, $len);
$datLen += $len;
if($this->m_gih->m_bInterlace) {
$this->deInterlace();
}
return true;
case 0x3B: // EOF
default:
return false;
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////
function skipExt(&$data, &$extLen)
{
$extLen = 0;
$b = ord($data{0});
$data = substr($data, 1);
$extLen++;
switch($b) {
case 0xF9: // Graphic Control
$b = ord($data{1});
$this->m_disp = ($b & 0x1C) >> 2;
$this->m_bUser = ($b & 0x02) ? true : false;
$this->m_bTrans = ($b & 0x01) ? true : false;
$this->m_nDelay = $this->w2i(substr($data, 2, 2));
$this->m_nTrans = ord($data{4});
break;
case 0xFE: // Comment
$this->m_lpComm = substr($data, 1, ord($data{0}));
break;
case 0x01: // Plain text
break;
case 0xFF: // Application
break;
}
// SKIP DEFAULT AS DEFS MAY CHANGE
$b = ord($data{0});
$data = substr($data, 1);
$extLen++;
while($b > 0) {
$data = substr($data, $b);
$extLen += $b;
$b = ord($data{0});
$data = substr($data, 1);
$extLen++;
}
return true;
}
///////////////////////////////////////////////////////////////////////////
function w2i($str)
{
return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
}
///////////////////////////////////////////////////////////////////////////
function deInterlace()
{
$data = $this->m_data;
for($i = 0; $i < 4; $i++) {
switch($i) {
case 0:
$s = 8;
$y = 0;
break;
case 1:
$s = 8;
$y = 4;
break;
case 2:
$s = 4;
$y = 2;
break;
case 3:
$s = 2;
$y = 1;
break;
}
for(; $y < $this->m_gih->m_nHeight; $y += $s) {
$lne = substr($this->m_data, 0, $this->m_gih->m_nWidth);
$this->m_data = substr($this->m_data, $this->m_gih->m_nWidth);
$data =
substr($data, 0, $y * $this->m_gih->m_nWidth) .
$lne .
substr($data, ($y + 1) * $this->m_gih->m_nWidth);
}
}
$this->m_data = $data;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
class CGIF
{
var $m_gfh;
var $m_lpData;
var $m_img;
var $m_bLoaded;
///////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
function CGIF()
{
$this->m_gfh = new CGIFFILEHEADER();
$this->m_img = new CGIFIMAGE();
$this->m_lpData = "";
$this->m_bLoaded = false;
}
///////////////////////////////////////////////////////////////////////////
function loadFile($lpszFileName, $iIndex)
{
if($iIndex < 0) {
return false;
}
// READ FILE
if(!($fh = @fOpen($lpszFileName, "rb"))) {
return false;
}
//EDITEI - in order to read remote files (HTTP(s) and FTP protocols)
if ( strpos($lpszFileName,"http") !== false or strpos($lpszFileName,"ftp") !== false )
{
$contents = '';
while (!feof($fh)) $contents .= @fread($fh, 8192);
}
else
{
$contents = @fread($fh,@filesize($lpszFileName) );
}
$this->m_lpData = $contents;
// $this->m_lpData = @fRead($fh, @fileSize($lpszFileName));
fClose($fh);
// GET FILE HEADER
if(!$this->m_gfh->load($this->m_lpData, $len = 0)) {
return false;
}
$this->m_lpData = substr($this->m_lpData, $len);
do {
if(!$this->m_img->load($this->m_lpData, $imgLen = 0)) {
return false;
}
$this->m_lpData = substr($this->m_lpData, $imgLen);
}
while($iIndex-- > 0);
$this->m_bLoaded = true;
return true;
}
///////////////////////////////////////////////////////////////////////////
function getSize($lpszFileName, &$width, &$height)
{
if(!($fh = @fOpen($lpszFileName, "rb"))) {
return false;
}
$data = @fRead($fh, @fileSize($lpszFileName));
@fClose($fh);
$gfh = new CGIFFILEHEADER();
if(!$gfh->load($data, $len = 0)) {
return false;
}
$width = $gfh->m_nWidth;
$height = $gfh->m_nHeight;
return true;
}
///////////////////////////////////////////////////////////////////////////
function getBmp($bgColor)
{
$out = "";
if(!$this->m_bLoaded) {
return false;
}
// PREPARE COLOR TABLE (RGBQUADs)
if($this->m_img->m_gih->m_bLocalClr) {
$nColors = $this->m_img->m_gih->m_nTableSize;
$rgbq = $this->m_img->m_gih->m_colorTable->toRGBQuad();
if($bgColor != -1) {
$bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
}
}
else if($this->m_gfh->m_bGlobalClr) {
$nColors = $this->m_gfh->m_nTableSize;
$rgbq = $this->m_gfh->m_colorTable->toRGBQuad();
if($bgColor != -1) {
$bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
}
}
else {
$nColors = 0;
$bgColor = -1;
}
// PREPARE BITMAP BITS
$data = $this->m_img->m_data;
$nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth;
$bmp = "";
$nPad = ($this->m_gfh->m_nWidth % 4) ? 4 - ($this->m_gfh->m_nWidth % 4) : 0;
for($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
for($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
if(
($x >= $this->m_img->m_gih->m_nLeft) &&
($y >= $this->m_img->m_gih->m_nTop) &&
($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) &&
($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) {
// PART OF IMAGE
if($this->m_img->m_bTrans && (ord($data{$nPxl}) == $this->m_img->m_nTrans)) {
// TRANSPARENT -> BACKGROUND
if($bgColor == -1) {
$bmp .= chr($this->m_gfh->m_nBgColor);
}
else {
$bmp .= chr($bgColor);
}
}
else {
$bmp .= $data{$nPxl};
}
}
else {
// BACKGROUND
if($bgColor == -1) {
$bmp .= chr($this->m_gfh->m_nBgColor);
}
else {
$bmp .= chr($bgColor);
}
}
}
$nPxl -= $this->m_gfh->m_nWidth << 1;
// ADD PADDING
for($x = 0; $x < $nPad; $x++) {
$bmp .= "\x00";
}
}
// BITMAPFILEHEADER
$out .= "BM";
$out .= $this->dword(14 + 40 + ($nColors << 2) + strlen($bmp));
$out .= "\x00\x00";
$out .= "\x00\x00";
$out .= $this->dword(14 + 40 + ($nColors << 2));
// BITMAPINFOHEADER
$out .= $this->dword(40);
$out .= $this->dword($this->m_gfh->m_nWidth);
$out .= $this->dword($this->m_gfh->m_nHeight);
$out .= "\x01\x00";
$out .= "\x08\x00";
$out .= "\x00\x00\x00\x00";
$out .= "\x00\x00\x00\x00";
$out .= "\x12\x0B\x00\x00";
$out .= "\x12\x0B\x00\x00";
$out .= $this->dword($nColors % 256);
$out .= "\x00\x00\x00\x00";
// COLOR TABLE
if($nColors > 0) {
$out .= $rgbq;
}
// DATA
$out .= $bmp;
return $out;
}
///////////////////////////////////////////////////////////////////////////
function getPng($bgColor)
{
$out = "";
if(!$this->m_bLoaded) {
return false;
}
// PREPARE COLOR TABLE (RGBQUADs)
if($this->m_img->m_gih->m_bLocalClr) {
$nColors = $this->m_img->m_gih->m_nTableSize;
$pal = $this->m_img->m_gih->m_colorTable->toString();
if($bgColor != -1) {
$bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
}
}
else if($this->m_gfh->m_bGlobalClr) {
$nColors = $this->m_gfh->m_nTableSize;
$pal = $this->m_gfh->m_colorTable->toString();
if($bgColor != -1) {
$bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
}
}
else {
$nColors = 0;
$bgColor = -1;
}
// PREPARE BITMAP BITS
$data = $this->m_img->m_data;
$nPxl = 0;
$bmp = "";
for($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
$bmp .= "\x00";
for($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
if(
($x >= $this->m_img->m_gih->m_nLeft) &&
($y >= $this->m_img->m_gih->m_nTop) &&
($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) &&
($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) {
// PART OF IMAGE
$bmp .= $data{$nPxl};
}
else {
// BACKGROUND
if($bgColor == -1) {
$bmp .= chr($this->m_gfh->m_nBgColor);
}
else {
$bmp .= chr($bgColor);
}
}
}
}
$bmp = gzcompress($bmp, 9);
///////////////////////////////////////////////////////////////////////
// SIGNATURE
$out .= "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
///////////////////////////////////////////////////////////////////////
// HEADER
$out .= "\x00\x00\x00\x0D";
$tmp = "IHDR";
$tmp .= $this->ndword($this->m_gfh->m_nWidth);
$tmp .= $this->ndword($this->m_gfh->m_nHeight);
$tmp .= "\x08\x03\x00\x00\x00";
$out .= $tmp;
$out .= $this->ndword(crc32($tmp));
///////////////////////////////////////////////////////////////////////
// PALETTE
if($nColors > 0) {
$out .= $this->ndword($nColors * 3);
$tmp = "PLTE";
$tmp .= $pal;
$out .= $tmp;
$out .= $this->ndword(crc32($tmp));
}
///////////////////////////////////////////////////////////////////////
// TRANSPARENCY
if($this->m_img->m_bTrans && ($nColors > 0)) {
$out .= $this->ndword($nColors);
$tmp = "tRNS";
for($i = 0; $i < $nColors; $i++) {
$tmp .= ($i == $this->m_img->m_nTrans) ? "\x00" : "\xFF";
}
$out .= $tmp;
$out .= $this->ndword(crc32($tmp));
}
///////////////////////////////////////////////////////////////////////
// DATA BITS
$out .= $this->ndword(strlen($bmp));
$tmp = "IDAT";
$tmp .= $bmp;
$out .= $tmp;
$out .= $this->ndword(crc32($tmp));
///////////////////////////////////////////////////////////////////////
// END OF FILE
$out .= "\x00\x00\x00\x00IEND\xAE\x42\x60\x82";
return $out;
}
///////////////////////////////////////////////////////////////////////////
function dword($val)
{
$val = intval($val);
return chr($val & 0xFF).chr(($val & 0xFF00) >> 8).chr(($val & 0xFF0000) >> 16).chr(($val & 0xFF000000) >> 24);
}
///////////////////////////////////////////////////////////////////////////
function ndword($val)
{
$val = intval($val);
return chr(($val & 0xFF000000) >> 24).chr(($val & 0xFF0000) >> 16).chr(($val & 0xFF00) >> 8).chr($val & 0xFF);
}
///////////////////////////////////////////////////////////////////////////
function width()
{
return $this->m_gfh->m_nWidth;
}
///////////////////////////////////////////////////////////////////////////
function height()
{
return $this->m_gfh->m_nHeight;
}
///////////////////////////////////////////////////////////////////////////
function comment()
{
return $this->m_img->m_lpComm;
}
///////////////////////////////////////////////////////////////////////////
function loaded()
{
return $this->m_bLoaded;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -