📄 remote.php
字号:
}
// Extract client error
if (preg_match("/^HTTP\/1\.[0-1] (4[0-9][0-9] .*)/", $line, $matches)) {
$this->errstr = $matches[1];
$retval = 0;
}
// Extract server error
if (preg_match("/^HTTP\/1\.[0-1] (5[0-9][0-9] .*)/", $line, $matches)) {
$this->errstr = $matches[1];
$retval = 0;
}
// Extract mime type
if (preg_match("/Content-Type: (.*)/", $line, $matches)) {
$tempmime = chop($matches[1]);
}
// Extract filesize
if (preg_match("/Content-Length: ([0-9]*)/", $line, $matches)) {
$tempfilesize = $matches[1];
}
$line = trim(fgets($fp,128));
}
// If we got correct mimetype etc (we trust what the webserver says),
// continue to fetch the actual data.
// if no error yet
if ($retval) {
// if correct mimetype
if (preg_match("/image\/(gif|jpeg|x-jpeg|x-png|png)/", $tempmime)) {
$this->mimetype = $tempmime;
$this->filesize = $tempfilesize;
SWITCH ($tempmime) {
case 'image/gif':
Image_Remote::_fetchAndParseGIFHeader($fp);
$this->imagetype = 1;
break;
case 'image/png':
case 'image/x-png':
Image_Remote::_fetchAndParsePNGHeader($fp);
$this->imagetype = 3;
break;
case 'image/jpeg':
case 'image/x-jpeg':
Image_Remote::_fetchAndParseJPGHeader($fp);
$this->imagetype = 2;
break;
}
} else {
$this->errstr = "Unsupported mimetype $tempmime.";
$retval = 0;
}
}
fclose($fp);
}
return($retval);
} // _fetchImageInfo
/**
* Parse GIF header
*
* @access private
* @return void
*/
function _fetchAndParseGIFHeader($fd)
{
if (!$fd) {
$this->errstr = "No socket.";
} else {
$signature = fread($fd, GIF_SIGNATURE_LENGTH);
$version = fread($fd, GIF_VERSION_LENGTH);
$wbytes = fread($fd, GIF_LSW_LENGTH);
$hbytes = fread($fd, GIF_LSH_LENGTH);
// Host Byte Order
$this->width = Image_Remote::_htodecs($wbytes);
$this->height = Image_Remote::_htodecs($hbytes);
$this->extra = $signature . $version;
}
}
/**
* Parse PNG header
*
* @access private
* @return void
*/
function _fetchAndParsePNGHeader($fd)
{
if (!$fd) {
$this->errstr = "No socket.";
} else {
$signature = fread($fd, PNG_SIGNATURE_LENGTH);
$ihdrl = fread($fd, PNG_IHDR_LENGTH_LENGTH);
$ihdrct = fread($fd, PNG_IHDR_CHUNKTYPE_LENGTH);
$wbytes = fread($fd, PNG_IHDR_IMW_LENGTH);
$hbytes = fread($fd, PNG_IHDR_IMH_LENGTH);
// Network Byte Order
$this->width = Image_Remote::_ntodecl($wbytes);
$this->height = Image_Remote::_ntodecl($hbytes);
$this->extra = $signature;
}
}
// The jpeg parser is basically port of code found from rdjpgcom.c,
// which is part of the Independent JPEG Group's software.
// Copyright (C) 1994-1997, Thomas G. Lane.
//
// ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz
//
// Similar port can be found in elqGetImageFormat() by Oyvind Hallsteinsen
//
// ftp://ftp.elq.org/pub/php3/elqGetImageFormat/elqGetImageFormat.php3
/**
* Parse JPEG header
*
* @access private
* @return void
*/
function _fetchAndParseJPGHeader($fd)
{
if (!$fd) {
$this->errstr = "No socket.";
} else {
$done = 0;
// first marker
$c1 = ord(fread($fd, 1));
$c2 = ord(fread($fd, 1));
if ($c1 != 0xFF || $c2 != M_SOI) {
$this->errstr="Not a jpeg file?";
} else {
while (!($done)) {
// find next marker
$marker = ord(fread($fd, 1));
while ($marker != 0xFF) {
$marker = ord(fread($fd, 1));
}
do {
$marker = ord(fread($fd, 1));
} while ($marker == 0xFF);
SWITCH ($marker) {
case M_SOF0:
case M_SOF1:
case M_SOF2:
case M_SOF3:
case M_SOF5:
case M_SOF6:
case M_SOF7:
case M_SOF9:
case M_SOF10:
case M_SOF11:
case M_SOF13:
case M_SOF14:
case M_SOF15:
$length = fread($fd, JPG_PARAM_LENGTH_LENGTH);
$precision = fread($fd, JPG_DATA_PRECISION_LENGTH);
$hbytes = fread($fd, JPG_IMH_LENGTH_LENGTH);
$wbytes = fread($fd, JPG_IMW_LENGTH_LENGTH);
$components= fread($fd, JPG_NUM_COMPONENTS_LENGTH);
// Network Byte Order
$this->width = Image_Remote::_ntodecs($wbytes);
$this->height = Image_Remote::_ntodecs($hbytes);
$this->extra = "";
$done = 1;
break;
case M_SOS:
$done = 1;
break;
// By default we skip over unwanted markers and avoid
// being fooled by 0xFF bytes in parameter segment.
default:
$lbytes = fread($fd, JPG_PARAM_LENGTH_LENGTH);
$length = Image_Remote::_ntodecs($lbytes);
if ($length < JPG_PARAM_LENGTH_LENGTH) {
$this->errstr="Erronous parameter length.";
$done = 1;
}
// the length is included in param length and
// allready read
$length -= JPG_PARAM_LENGTH_LENGTH;
fread($fd, $length);
break;
}
}
}
}
}
/**
* Host byte order to decimal long
*
* @access private
* @return integer
*/
function _htodecl($bytes)
{
$b1 = ord($bytes[0]);
$b2 = (ord($bytes[1])<<8);
$b3 = (ord($bytes[2])<<16);
$b4 = (ord($bytes[3])<<24);
return($b1 + $b2 + $b3 + $b4);
}
/**
* Host byte order to decimal short
*
* @access private
* @return integer
*/
function _htodecs($bytes)
{
$b1 = ord($bytes[0]);
$b2 = (ord($bytes[1]) << 8);
return($b1 + $b2);
}
/**
* Network byte order to decimal long
*
* @access private
* @return integer
*/
function _ntodecl($bytes)
{
$b1 = ord($bytes[3]);
$b2 = (ord($bytes[2]) << 8);
$b3 = (ord($bytes[1]) << 16);
$b4 = (ord($bytes[0]) << 24);
return($b1 + $b2 + $b3 + $b4);
}
/**
* Network byte order to decimal short
*
* @access private
* @return integer
*/
function _ntodecs($bytes)
{
$b1 = ord($bytes[1]);
$b2 = (ord($bytes[0]) << 8 );
return($b1 + $b2);
}
} // class
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -