sub jpegsize {
my $file = shift;
open (JPEG,"<",$file) or die $!;
my $jpeg = join "",<JPEG>;
close JPEG;
my ($count)=2;
my ($length)=length($jpeg);
my ($ch)="";
my ($c1,$c2,$x,$y,$c,$d,$width,$height);
while (($ch ne "\xda") && ($count<$length)) {
while (($ch ne "\xff") && ($count < $length)) {
$ch=substr($jpeg,$count,1);
$count++;
}
while (($ch eq "\xff") && ($count<$length)) {
$ch=substr($jpeg,$count,1);
$count++;
}
if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
$count+=3;
($x,$y,$c,$d)=unpack("C"x4,substr($jpeg,$count,4));
$width=$c<<8|$d;
$height=$x<<8|$y;
return($width,$height);
} else {
($c1,$c2)= unpack("C"x2,substr($jpeg,$count,2));
$count += $c1<<8|$c2;
}
}
undef;
}