A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [Resolved] [Resolved] [Resolved] [Resolved] [Resolved] [Resolved] [Resolved] Perl and Image

  1. #1
    Senior Member
    Join Date
    Oct 2000
    Posts
    518

    resolved

    Can I check the dimensions of an image by using Perl?

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    Hi,

    you can write a script like this for gif images
    sub gifsize
    { my $f = shift;
    open F, $f;
    read F, $buf, 6;
    return { status => 0, error => "not gif" } if($buf !~ /GIF8[79]a/);
    read F, $buf, 4;
    ($w, $h) = unpack("vv", $buf);
    close F;
    return { status => 1, size = -s _, width => $w, height => $h };
    }
    and similar functions for jpg, swf or other formats you care about

    Musicman

  3. #3
    Senior Member
    Join Date
    Oct 2000
    Posts
    518

    resolved

    Do you mind providing a little explanation? Even though I know Perl, but err.......I don't understand what you are writing......

    The only thing I know is that you're writing a subroutine.....

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    sub gifsize
    { my $f = shift; ## call as $imgdata = gifsize($filename), $f becomes local copy of $filename
    open F, $f; ## open that file ... assume its existence has already been tested
    binmode F; ## tell windows it is binary file, not required for other systems
    read F, $buf, 6; ## read first 6 bytes of file header, should be GIF87a or GIF89a
    return { status => 0, error => "not gif" } if($buf !~ /GIF8[79]a/); ## otherwise return "bad" result
    read F, $buf, 4; ## read four more binary bytes, these are the image width and height in "vax" byte order
    ($w, $h) = unpack("vv", $buf); ## returns unpack result as a list
    close F; ## done with that file
    return { status => 1, width => $w, height => $h }; ## return "good" result as a hash reference
    }
    I removed the filesize here, because the whole thing would probably by used like
    if(-r $imgfile) ## file readable at all
    { $imgdata = gifsize($imgname) if($imgname =~/\.gif$/i);
    $imgdata = jpegsize($imgname) if($imgname =~/\.jpe?g$/i);

    ...
    if(defined($imgdata) && $$imgdata{status})
    { ## image could be deciphered
    $$imgdata{size} = -s _;
    print "$filename is a " . $$imgdata{width} "x" $$imgdata{height} . " image, size " . $$imgdata{size} . " bytes\n";

    You could return hashes rather than hash references, like
    return ('status', 1, 'width', $w, 'height', $h);
    and then access the results as $imgdata{width} rather than $$imgdata{width}
    For details of pack / unpack function please use man perl or read the camel book

    Musicman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center