Source for file Remote.php
Documentation is available at Remote.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Mika Tuupola <tuupola@appelsiini.net> |
// +----------------------------------------------------------------------+
// $Id: Remote.php,v 1.3 2003/01/03 15:25:35 tuupola Exp $
define("GIF_SIGNATURE_LENGTH", 3 );
define("GIF_VERSION_LENGTH", 3 );
define("PNG_SIGNATURE_LENGTH", 8 );
define("PNG_IHDR_LENGTH_LENGTH", 4 );
define("PNG_IHDR_CHUNKTYPE_LENGTH", 4 );
define("PNG_IHDR_IMW_LENGTH", 4 );
define("PNG_IHDR_IMH_LENGTH", 4 );
define("JPG_PARAM_LENGTH_LENGTH", 2 );
define("JPG_DATA_PRECISION_LENGTH", 1 );
define("JPG_IMW_LENGTH_LENGTH", 2 );
define("JPG_IMH_LENGTH_LENGTH", 2 );
define("JPG_NUM_COMPONENTS_LENGTH", 1 );
// The following jpeg defines were taken 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
define("M_SOF0", 0xC0 ); /* Start Of Frame N */
define("M_SOF1", 0xC1 ); /* N indicates which compression process */
define("M_SOF2", 0xC2 ); /* Only SOF0-SOF2 are now in common use */
define("M_SOF5", 0xC5 ); /* NB: codes C4 and CC are NOT SOF markers */
define("M_SOI", 0xD8 ); /* Start Of Image (beginning of datastream) */
define("M_EOI", 0xD9 ); /* End Of Image (end of datastream) */
define("M_SOS", 0xDA ); /* Start Of Scan (begins compressed data) */
define("M_APP0", 0xE0 ); /* Application-specific marker, type N */
define("M_APP12", 0xEC ); /* (we don't bother to list all 16 APPn's) */
define("M_COM", 0xFE ); /* COMment */
require_once('PEAR.php');
* Image_Remote - retrieve size from remote image files.
* Class can be used to get size information from remote
* image files without downloading the whole image file.
* It mimics the GetImageSize() PHP function but works on
* remote images. Gif, jpeg and png currently supported.
* NOTE: Since PHP 4.0.5 URL support was added also to PHP's native
* $i = new Image_Remote("http://www.example.com/test.jpg");
* if (PEAR::isError($i)) {
* print "ERROR: " . $i->getMessage();
* $size = $i->getImageSize();
* $comment = $i->getImageComment();
* @author Mika Tuupola <tuupola@appelsiini.net>
* @version $Revision: 1.3 $
* The URL to fetch size from
* Filesize of the remote image
var $filesize; // filesize of the remote file
* Mimetype of the remote image
* Imagetype of the remote image
* Width of the remote image
var $width; // image width
* Height of the remote image
var $height; // image height
var $extra; // some useless extrainfo
* Error of the last method call
var $errstr; // error of the last method call
* @param string foo URL of the remote image. Currently supports only http.
function Image_Remote ($input)
if ($this->purl[scheme ] == 'http') {
// assign default port if not given
if (!($this->purl[port ])) {
// actually fetch the info
Image_Remote ::_fetchImageInfo ();
$this->errstr = "Only http supported.";
$this = new PEAR_Error ($this->errstr);
* Return information on the remote image.
* Imitates PHP's native GetImageSize.
* http://www.php.net/manual/function.getimagesize.php
$retval = array (0 ,0 ,0 ,"");
$retval[0 ] = $this->width;
$retval[1 ] = $this->height;
$retval[2 ] = $this->imagetype;
$retval[3 ] = " WIDTH=\"$retval[0]\" HEIGHT=\"$retval[1]\"";
* Return the URL of the remote image.
* This is actually the same URL which was given to the constructor.
* However it sometimes comes handy to be able to retrieve the URL
* Return the Comment found in the remote image.
* This comment is being used to specify the JPG image.
* http://www.w3.org/Graphics/JPEG/itu-t81.pdf
* @author Urs Gehrig <urs@circle.ch>
function getImageComment ()
$retval = $this->comment;
* Fetch information of the remote image.
function _fetchImageInfo ()
// Open the connection and get the headers
$fp = fsockopen ($this->purl[host ], $this->purl[port ], &$errno, &$errstr, 30 );
$this->errstr = " Could not open socket $errstr ($errno)\n";
fputs ($fp, "GET " . $this->purl[path ] . $this->purl[query ] . " HTTP/1.1\r\n");
fputs ($fp, "Host: " . $this->purl[host ] . "\r\n");
fputs ($fp, "User-Agent: PEAR Image_Remote class ( http://pear.php.net) \r\n");
fputs ($fp, "Connection: close\r\n\r\n");
if (preg_match("/^HTTP\/1\.[0-1] (3[0-9][0-9] .*)/", $line, $matches)) {
$this->errstr = $matches[1 ] . " not implemented yet!";
if (preg_match("/^HTTP\/1\.[0-1] (4[0-9][0-9] .*)/", $line, $matches)) {
$this->errstr = $matches[1 ];
if (preg_match("/^HTTP\/1\.[0-1] (5[0-9][0-9] .*)/", $line, $matches)) {
$this->errstr = $matches[1 ];
if (preg_match("/Content-Type: (.*)/", $line, $matches)) {
$tempmime = chop($matches[1 ]);
if (preg_match("/Content-Length: ([0-9]*)/", $line, $matches)) {
$tempfilesize = $matches[1 ];
// If we got correct mimetype etc (we trust what the webserver says),
// continue to fetch the actual data.
if (preg_match("/image\/(gif|jpeg|x-jpeg|x-png|png)/", $tempmime)) {
$this->mimetype = $tempmime;
$this->filesize = $tempfilesize;
Image_Remote ::_fetchAndParseGIFHeader ($fp);
Image_Remote ::_fetchAndParsePNGHeader ($fp);
Image_Remote ::_fetchAndParseJPGHeader ($fp);
$this->errstr = " Unsupported mimetype $tempmime.";
function _fetchAndParseGIFHeader ($fd)
$this->errstr = "No socket.";
$this->width = Image_Remote ::_htodecs ($wbytes);
$this->height = Image_Remote ::_htodecs ($hbytes);
$this->extra = $signature . $version;
function _fetchAndParsePNGHeader ($fd)
$this->errstr = "No socket.";
$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
function _fetchAndParseJPGHeader ($fd)
$this->errstr = "No socket.";
if ($c1 != 0xFF || $c2 != M_SOI) {
$this->errstr= "Not a jpeg file?";
while ($marker != 0xFF ) {
} while ($marker == 0xFF );
$this->width = Image_Remote ::_ntodecs ($wbytes);
$this->height = Image_Remote ::_ntodecs ($hbytes);
$length = Image_Remote ::_chartodec ($bytes);
$this->comment = fread($fd, $length);
// By default we skip over unwanted markers and avoid
// being fooled by 0xFF bytes in parameter segment.
$length = Image_Remote ::_ntodecs ($lbytes);
$this->errstr= "Erronous parameter length.";
// the length is included in param length and
* Host byte order to decimal long
function _htodecl ($bytes)
$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
function _htodecs ($bytes)
$b2 = (ord($bytes[1 ]) << 8 );
* Network byte order to decimal long
function _ntodecl ($bytes)
$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
function _ntodecs ($bytes)
$b2 = (ord($bytes[0 ]) << 8 );
* Convert char to decimal
* @author Urs Gehrig <urs@circle.ch>
function _chartodec ($bytes)
$b1 = (ord($bytes[0 ]) << 8 );
Documentation generated on Mon, 11 Mar 2019 15:29:19 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|