##################################### # Configuration for subroutine call # ##################################### # Sample of HOW to USE No 1. # Path to images directory my $path = "../html/images/"; # Name of file my $file = "1_500.jpg"; # Call the request for width and height my ($width , $height ) = &imgsize("$path$file"); print " Height = $height \n Width = $width \n"; # Sample of HOW to USE No 2. $file = "test.gif"; # Call the request for width and height ($width , $height ) = &imgsize("$path$file"); print " Height = $height \n Width = $width \n"; ##################################### # Subroutines from here down # ##################################### sub imgsize { my($file)= shift @_; my($x,$y)=(0,0); if( defined($file) && open(STRM, "<$file") ){ binmode( STRM ); # for crappy MS OSes - Win/Dos/NT use is NOT SUPPORTED if ($file =~ /\.jpg$/i || $file =~ /\.jpeg$/i) { ($x,$y) = &jpegsize(\*STRM); } elsif($file =~ /\.gif$/i) { ($x,$y) = &gifsize(\*STRM); } elsif($file =~ /\.xbm$/i) { ($x,$y) = &xbmsize(\*STRM); } elsif($file =~ /\.png$/i) { ($x,$y) = &pngsize(\*STRM); } else { print "$file is not gif, xbm, jpeg or png (or has stupid name)"; } close(STRM); } return ($x,$y); } ########################################################################### # Subroutine gets the size of the specified file ########################################################################### sub gifsize { my($GIF) = @_; my($type,$a,$b,$c,$d,$s)=(0,0,0,0,0,0); if(defined( $GIF ) && read($GIF, $type, 6) && $type =~ /GIF8[7,9]a/ && read($GIF, $s, 4) == 4 ){ ($a,$b,$c,$d)=unpack("C"x4,$s); return ($b<<8|$a,$d<<8|$c); } return (0,0); } sub xbmsize { my($XBM) = @_; my($input)=""; if( defined( $XBM ) ){ $input .= <$XBM>; $input .= <$XBM>; $input .= <$XBM>; $_ = $input; if( /.define\s+\S+\s+(\d+)\s*\n.define\s+\S+\s+(\d+)\s*\n/i ){ return ($1,$2); } } return (0,0); } # pngsize : gets the width & height (in pixels) of a png file # cor this program is on the cutting edge of technology! (pity it's blunt!) # GRR 970619: fixed bytesex assumption sub pngsize { my($PNG) = @_; my($head) = ""; # my($x,$y); my($a, $b, $c, $d, $e, $f, $g, $h)=0; if(defined($PNG) && read( $PNG, $head, 8 ) == 8 && $head eq "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a" && read($PNG, $head, 4) == 4 && read($PNG, $head, 4) == 4 && $head eq "IHDR" && read($PNG, $head, 8) == 8 ){ # ($x,$y)=unpack("I"x2,$head); # doesn't work on little-endian machines # return ($x,$y); ($a,$b,$c,$d,$e,$f,$g,$h)=unpack("C"x8,$head); return ($a<<24|$b<<16|$c<<8|$d, $e<<24|$f<<16|$g<<8|$h); } return (0,0); } # jpegsize : gets the width and height (in pixels) of a jpeg file # Andrew Tong, werdna@ugcs.caltech.edu February 14, 1995 # modified slightly by alex@ed.ac.uk sub jpegsize { my($JPEG) = @_; my($done)=0; my($c1,$c2,$ch,$s,$length, $dummy)=(0,0,0,0,0,0); my($a,$b,$c,$d); if(defined($JPEG) && read($JPEG, $c1, 1) && read($JPEG, $c2, 1) && ord($c1) == 0xFF && ord($c2) == 0xD8 ){ while (ord($ch) != 0xDA && !$done) { # Find next marker (JPEG markers begin with 0xFF) # This can hang the program!! while (ord($ch) != 0xFF) { return(0,0) unless read($JPEG, $ch, 1); } # JPEG markers can be padded with unlimited 0xFF's while (ord($ch) == 0xFF) { return(0,0) unless read($JPEG, $ch, 1); } # Now, $ch contains the value of the marker. if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) { return(0,0) unless read ($JPEG, $dummy, 3); return(0,0) unless read($JPEG, $s, 4); ($a,$b,$c,$d)=unpack("C"x4,$s); return ($c<<8|$d, $a<<8|$b ); } else { # We **MUST** skip variables, since FF's within variable names are # NOT valid JPEG markers return(0,0) unless read ($JPEG, $s, 2); ($c1, $c2) = unpack("C"x2,$s); $length = $c1<<8|$c2; last if (!defined($length) || $length < 2); read($JPEG, $dummy, $length-2); } } } return (0,0); } 1;