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".


In reply to Re: Image Size (Some fun functions) by gmpassos
in thread Image Size by andrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.