Important alert: (current site time 7/15/2013 8:49:53 PM EDT)
 

VB icon

ImageSize

Email
Submitted on: 12/6/2001 2:32:14 AM
By: M B 
Level: Advanced
User Rating: By 3 Users
Compatibility: 5.0 (all versions)
Views: 10663
 
     Gets the size of a Gif, JPG or Jpeg file
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
=**************************************
= for :ImageSize
=**************************************
You can use, share or do wathever with this code you want to do.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: ImageSize
= Description:Gets the size of a Gif, JPG or Jpeg file
= By: M B
=
= Inputs:$File=Filename
=
= Returns:Size of the image
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=259&lngWId=6=for details.=**************************************

#!D:\PERL\BIN\PERL.EXE
		
#	Developed by: Merijn Boom	
&imgsize("c:/sample.jpg");
sub imgsize {
 my($file)= shift @_;
 my($ref)=@_ ? shift @_ : "";
 my($x,$y)=(0,0);
	defined($file) && open(STRM, "<$file");
if ($file =~ /\.jpg$/i || $file =~ /\.jpeg$/i) {
 ($x,$y) = &jpegsize(\*STRM);
} elsif($file =~ /\.gif$/i) {
 ($x,$y) = &gifsize(\*STRM);
} else {
 print "$file is not gif, jpg or jpeg file (or has strange name)";
}
close(STRM);
 
 print"$x, $y\n";
 return ($x,$y);
}
###########################################################################
# Subroutine gets the size of the specified GIF
###########################################################################
sub gifsize
{
 my($GIF) = @_;
 if( &istrue($UseNewGifsize) ){
return &NEWgifsize($GIF);
 } else {
return &OLDgifsize($GIF);
 }
}
###########################################################################
# Subroutine gets the size of the specified JPG or JPeG
###########################################################################
sub jpegsize {
		print".jpg\n";
 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);
}
sub istrue
{
 my( $val)=@_;
 return (defined($val) && ($val =~ /^y(es)?/i || $val =~ /true/i ));
}
sub OLDgifsize {
 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);
}
# part of NEWgifsize
sub gif_blockskip {
 my ($GIF, $skip, $type) = @_;
 my ($s)=0;
 my ($dummy)='';
 read ($GIF, $dummy, $skip);	# Skip header (if any)
 while (1) {
if (eof ($GIF)) {
 warn "Invalid/Corrupted GIF (at EOF in GIF $type)\n";
 return "";
}
read($GIF, $s, 1);		# Block size
last if ord($s) == 0;	# Block terminator
read ($GIF, $dummy, ord($s));	# Skip data
 }
}
sub NEWgifsize {
 my($GIF) = @_;
 my($cmapsize, $a, $b, $c, $d, $e)=0;
 my($type,$s)=(0,0);
 my($x,$y)=(0,0);
 my($dummy)='';
 return($x,$y) if(!defined $GIF);
 read($GIF, $type, 6);
 if($type !~ /GIF8[7,9]a/ || read($GIF, $s, 7) != 7 ){
warn "Invalid/Corrupted GIF (bad header)\n";
return($x,$y);
 }
 ($e)=unpack("x4 C",$s);
 if ($e & 0x80) {
$cmapsize = 3 * 2**(($e & 0x07) + 1);
if (!read($GIF, $dummy, $cmapsize)) {
 warn "Invalid/Corrupted GIF (global color map too small?)\n";
 return($x,$y);
}
 }
 FINDIMAGE:
 while (1) {
if (eof ($GIF)) {
 warn "Invalid/Corrupted GIF (at EOF w/o Image Descriptors)\n";
 return($x,$y);
}
read($GIF, $s, 1);
($e) = unpack("C", $s);
if ($e == 0x2c) {		# Image Descriptor (GIF87a, GIF89a 20.c.i)
 if (read($GIF, $s, 8) != 8) {
	warn "Invalid/Corrupted GIF (missing image header?)\n";
	return($x,$y);
 }
 ($a,$b,$c,$d)=unpack("x4 C4",$s);
 $x=$b<<8|$a;
 $y=$d<<8|$c;
 return($x,$y);
}
if ($type eq "GIF89a") {
 if ($e == 0x21) {		# Extension Introducer (GIF89a 23.c.i)
	read($GIF, $s, 1);
	($e) = unpack("C", $s);
	if ($e == 0xF9) {	# Graphic Control Extension (GIF89a 23.c.ii)
	 read($GIF, $dummy, 6);	# Skip it
	 next FINDIMAGE;	# Look again for Image Descriptor
	} elsif ($e == 0xFE) {	# Comment Extension (GIF89a 24.c.ii)
	 &gif_blockskip ($GIF, 0, "Comment");
	 next FINDIMAGE;	# Look again for Image Descriptor
	} elsif ($e == 0x01) {	# Plain Text Label (GIF89a 25.c.ii)
	 &gif_blockskip ($GIF, 12, "text data");
	 next FINDIMAGE;	# Look again for Image Descriptor
	} elsif ($e == 0xFF) {	# Application Extension Label (GIF89a 26.c.ii)
	 &gif_blockskip ($GIF, 11, "application data");
	 next FINDIMAGE;	# Look again for Image Descriptor
	} else {
	 printf STDERR "Invalid/Corrupted GIF (Unknown extension %#x)\n", $e;
	 return($x,$y);
	}
 }
 else {
	printf STDERR "Invalid/Corrupted GIF (Unknown code %#x)\n", $e;
	return($x,$y);
 }
}
else {
 warn "Invalid/Corrupted GIF (missing GIF87a Image Descriptor)\n";
 return($x,$y);
}
 }
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/2/2002 1:03:45 AM

This script is very handy if you do not have the ImageMagick package already installed. Having recently gotten into image manipulation this would have been most useful except that I needed to create thumbnails and the convert package from IM won out. Good job on the script however.
(If this comment was disrespectful, please report it.)

 
3/8/2006 1:36:18 PMBoi

how do you run the code?
I used ./filename.pl but it sends back the following error "-bash: ./boi.pl: USR\BIN\PERL: bad interpreter: No such file or directory" where boi.pl is the name of the fle. I will really appreciate your help.
Thank you!

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.