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

I need to be able to determine an image's (jpg/gif) size, but admin will not install a module (Image::Size, Image::Magick). Does anyone have or know of any stand-alone perl code to do just this?

Replies are listed 'Best First'.
Re: Determine Image Size without Modules
by bart (Canon) on Feb 20, 2003 at 01:54 UTC
    Image::Size is a Pure Perl module.

    In order to get it installed without make, remove its dependencies on AUTOLOAD. That process is even described in the README that comes with it.

    Or, you can pull the code you're interested it out of the module.

      bart, duh, thanks so much. I guess I never knew some modules were written in perl. I should have guessed that. I couldn't get it to work by using 'use lib "/path/to/my/local/lib"; but I took your second idea, extracted the code into a .pl file and called it as a require. That solved the project's last outstaning requirement. Thanks so much I bow before the monk gates ;> STmindfulORM www.mindfulinthestorm.org
Re: Determine Image Size without Modules
by pfaut (Priest) on Feb 20, 2003 at 01:50 UTC

    A Google search for graphic image file format turned up a bunch of sites including http://www.dcs.ed.ac.uk/home/mxr/gfx/ which seems fairly comprehensive. If you can decipher the file header formats from these documents you should be able to read it in with perl using sysread (make sure you use binmode especially if you are not on *nix). You can then read the header using unpack (see Pack/Unpack Tutorial (aka How the System Stores Data) for some assistance there).

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Determine Image Size without Modules
by jepri (Parson) on Feb 20, 2003 at 01:56 UTC
    Install the module in your home directory and point perl to it. Perl doesn't have to use modules in the system directories, it's just more convenient and space effiecient.

    In fact you can even override other perl modules with your own copy. Look up the 'use lib' pragma.

    Come to think of it, you can even install and use your own copy of perl.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: Determine Image Size without Modules
by jaco (Pilgrim) on Feb 20, 2003 at 16:03 UTC
    you can just open the file and parse the header out

    open(FILE, "$image"); binmode(FILE); my($kind, $tsize) = (0,0); read(FILE, $kind, 6); if($kind =~/GIF/){ read(FILE, $tsize,4); ($a, $b, $c, $d) = unpack("C" x4,$tsize); ($x, $y) = ($b<<8|$a, $d<<8|$c); }

    i'd imagine jpegs would be slightly more difficult, but not overly hard