this works to get the image dimensions remotely.
πben
<? #################################################
GetURLImageSize( $urlpic ) determines the
dimensions of local/remote URL pictures.
returns array with ( $width,$height,$type )
##
Thanks to: Oyvind Hallsteinsen aka Gosub / ELq - gosub@elq.org
for the original size determining code
##
PHP Hack by Filipe Laborde-Basto Oct 21/2000
FREELY DISTRIBUTABLE -- use at your sole discretion! π Enjoy.
(Not to be sold in commercial packages though, keep it free!)
#################################################
define(GIF_SIG, "\x47\x49\x46");
define(JPG_SIG, "\xff\xd8\xff");
define(PNG_SIG, "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a");
define(JPG_SOF0, "\xc0"); / Start Of Frame N /
define(JPG_SOF1, "\xc1"); / N indicates which compression process /
define(JPG_SOF2, "\xc2"); / Only SOF0-SOF2 are now in common use /
define(JPG_SOF3, "\xc3");
define(JPG_SOF5, "\xc5"); / NB: codes C4 and CC are NOT SOF markers /
define(JPG_SOF6, "\xc6");
define(JPG_SOF7, "\xc7");
define(JPG_SOF9, "\xc9");
define(JPG_SOF10, "\xca");
define(JPG_SOF11, "\xcb");
define(JPG_SOF13, "\xcd");
define(JPG_SOF14, "\xce");
define(JPG_SOF15, "\xcf");
define(JPG_EOI, "\xd9"); / End Of Image (end of datastream) /
define(JPG_SOS, "\xda"); / Start Of Scan - image data start /
define(RD_BUF, 512); / amount of data to initially read /
function GetURLImageSize( $urlpic ){
$fd= @fopen($urlpic,"r");
if($fd){
#read in 1k, enough for GIF,PNG.
#continue to read from file, if the JPG chunk exceeds this
$imgData = fread( $fd,RD_BUF );
if( substr($imgData,0,3)==GIF_SIG ){
$dim =unpack ("v2dim",substr($imgData,6,4) );
$width=$dim["dim1"]; $height=$dim["dim2"];
$type = 1;
} elseif( substr($imgData,0,8)==PNG_SIG ){
$dim =unpack ("N2dim",substr($imgData,16,8) );
$width=$dim["dim1"]; $height=$dim["dim2"];
$type = 3;
} elseif( substr($imgData,0,3)==JPG_SIG ){
################# JPG CHUNK SCAN ####################
$imgPos = 2; $type = 2; $buffer = RD_BUF-2;
while($imgPos < strlen($imgData)) {
/* synchronize to the marker 0xFF */
$imgPos=strpos(&$imgData,0xFF,$imgPos)+1;
$marker = $imgData[$imgPos];
do { $marker = ord($imgData[$imgPos++]); } while ($marker == 255);
/* find dimensions of block */
switch (chr($marker)) {
/* Grab width/height from SOF segment (these are acceptable chunk types) */
case JPG_SOF0: case JPG_SOF1: case JPG_SOF2:
case JPG_SOF3: case JPG_SOF5: case JPG_SOF6:
case JPG_SOF7: case JPG_SOF9: case JPG_SOF10:
case JPG_SOF11: case JPG_SOF12: case JPG_SOF13:
case JPG_SOF14: case JPG_SOF15:
$dim =unpack ("n2dim",substr($imgData,$imgPos+3,4) );
$height=$dim["dim1"]; $width=$dim["dim2"];
break 2; //found it so exit
case JPG_EOI:
case JPG_SOS:
return FALSE; /* End loop in case we find one of these markers */
default: /* We're not interested in other markers */
$skiplen = (ord($imgData[$imgPos++])<<8)+ord($imgData[$imgPos++])-2;
/* if the skip is more than what we've read in, read more */
$buffer -= $skiplen;
if( $buffer<512 ){ #if the buffer of data is too low,read more file.
$imgData .= fread( $fd,$skiplen+1024 );
$buffer += $skiplen+1024;
};
$imgPos += $skiplen;
break;
}; //endif check marker type
}; //endif loop through JPG chunks
}; //endif chk for valid file types
# got the pic dimensions, close the file
fclose ($fd); return array( $width,$height,$type );
} else {
return array( '','','' );
}; //endif valid file pointer chk
}; // end function
?>