These short routines will return the size of GIF, JPEG and PNG images without the need for external libraries.
#!/usr/bin/perl -w use strict; sub sizePNG { return unless $_[0]; my ($width, $height); ($width, $height) = unpack( "NN", $1 ) if $_[0] =~ /IHDR(........) +/; return ($width,$height); } sub sizeGIF { return unless $_[0]; my ($width, $height); ($width, $height) = unpack( "SS", $1 ) if $_[0] =~ /^GIF8..(....)/ +; return ($width,$height); } sub sizeJPG { return unless $_[0]; my ($width, $height); ($height,$width) = unpack( "nn", $1 ) if $_[0] =~ /\xFF\xC0...(... +.)/; return ($width,$height); } sub get_file { open FILE, $_[0] or die $!; binmode FILE; local $/; my $data = <FILE>; close FILE; return $data; } my $gif = 'c:/sample.gif'; my $data = get_file($gif); my @res = sizeGIF($data); print "$gif width $res[0] height $res[1]\n"; my $jpg = 'c:/sample.jpg'; $data = get_file($jpg); @res = sizeJPG($data); print "$jpg width $res[0] height $res[1]\n"; my $png = 'c:/sample.png'; $data = get_file($png); @res = sizePNG($data); print "$png width $res[0] height $res[1]\n";
In reply to Image size in pure perl by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |