andrew has asked for the wisdom of the Perl Monks concerning the following question:

I need to find out the size of an Image, width and height, is this possible? Is there a way to do it with no modules?

Replies are listed 'Best First'.
Re: Image Size
by dws (Chancellor) on Oct 07, 2002 at 19:14 UTC
    Is there a way to do it with no modules?

    Extract the appropriate parts of Image::Info and embed them in your script. It's a small module.

Re: Image Size
by zigdon (Deacon) on Oct 07, 2002 at 19:04 UTC
    Image::Info has worked for me in the past. But if you don't want to use modules, you'd have to learn how to read different formatted images and gleam the size info from the binary data. Doesn't sound like fun to me.

    -- Dan

Re: Image Size (Some fun functions)
by gmpassos (Priest) on Oct 08, 2002 at 03:46 UTC
    Well, I encourage you to use modules, but you can use this functions below for JPG and GIF! I put for SWF too! :-P

    To use just point the file path to the right function, like in the example. For SWF the function is good to get the Flash version!

    my ($w , $h , $type , $version) = IMG_jpg_size('/tmp/splash.jpg') ; print ">> $w , $h , $type , $version\n" ; ################# # IMG_OPEN_FILE # ################# sub IMG_open_file { my ( $img_fl ) = @_ ; open(IMGSTRM, $img_fl) ; binmode( IMGSTRM ); return( \*IMGSTRM ); } ############### # IMG_READBUF # ############### sub IMG_readbuf { my $buf ; read(@_[0], $buf, @_[1]) ; return( $buf ) ; } ####### # JPG # ####### sub IMG_jpg_size { my($JPEG) = IMG_open_file(@_) ; my($done)=0; my($c1,$c2,$ch,$s,$length, $dummy)=(0,0,0,0,0,0); my($a,$b,$c,$d); if ( defined($JPEG) && ($c1 = &IMG_readbuf($JPEG,1)) && ($c2 = &IMG_readbuf($JPEG,1)) && ord($c1) == 0xFF && ord($c2) == 0xD8 ) { while (ord($ch) != 0xDA && !$done) { while (ord($ch) != 0xFF) { return('error') unless ($ch = &IMG_re +adbuf($JPEG,1)) ;} while (ord($ch) == 0xFF) { return('error') unless ($ch = &IMG_re +adbuf($JPEG,1)) ;} if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) { return('error') unless ($dummy = &IMG_readbuf($JPEG,3)) ; return('error') unless ($s = &IMG_readbuf($JPEG,4)) ; ($a,$b,$c,$d)=unpack("C"x4,$s); return ($c<<8|$d, $a<<8|$b , 'jpg'); } else { return('error') unless ($s = &IMG_readbuf($JPEG,2)) ; ($c1, $c2) = unpack("C"x2,$s); $length = $c1<<8|$c2; last if (!defined($length) || $length < 2); $dummy = &IMG_readbuf($JPEG,$length-2); } } } return ('error'); } ####### # GIF # ####### sub IMG_gif_size { my($GIF) = IMG_open_file(@_) ; my($a,$b,$c,$d)=(0,0,0,0,0); my $type = &IMG_readbuf($GIF,6) ; my $s = &IMG_readbuf($GIF,4) ; if(defined( $GIF ) && $type =~ /GIF8[7,9]a/ && length($s) == 4 ) { ($a,$b,$c,$d)=unpack("C"x4,$s); return ($b<<8|$a,$d<<8|$c,'gif', "\L$type\E"); } return ('error'); } ####### # SWF # ####### sub IMG_swf_size { my($SWF) = IMG_open_file(@_) ; my $header = &IMG_readbuf($SWF,33) ; if (defined( $SWF ) && substr($header , 0 , 3) =~ /FWS/) { sub IMG_swf_bin2int { unpack("N", pack("B32", substr("0" x 32 . sh +ift, -32))); } my $ver = IMG_swf_bin2int(unpack 'B8', substr($header, 3, 1)); my $bs = unpack 'B133', substr($header, 8, 17); my $bits = IMG_swf_bin2int(substr($bs, 0, 5)); my $x = int(IMG_swf_bin2int(substr($bs, 5+$bits, $bits))/20); my $y = int(IMG_swf_bin2int(substr($bs, 5+$bits*3, $bits))/20); return ($x, $y , 'swf' , $ver); } return ('error'); } ####### # END # ####### 1;

    Graciliano M. P.
    "The creativity is the expression of the liberty".

Re: Image Size
by vek (Prior) on Oct 07, 2002 at 19:21 UTC
    I was going to recommend Image::Size as I have used it with great success - but then I saw you wanted to do this without modules. Hmm, should probably go with dws's suggestion.

    -- vek --
A reply falls below the community's threshold of quality. You may see it by logging in.