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

I am writing a CGI script in which I need to check the size of an image that the user has submitted a URL for. Is there anyway to check the size (width and height) of the image? Also the image will probably be on a different site to mine. I was thinking maybe I could request the image using sockets and then check the size using GD but I am not sure how to do this or if this is the right way to go about it. Any help would be much appreciated.

Replies are listed 'Best First'.
(ar0n) Re: Checking Image Size
by ar0n (Priest) on Nov 26, 2001 at 23:20 UTC
    The easiest way to do it would probably be by using LWP::Simple (see 'get') and Image::Size (using 'imgsize'):
    #!/usr/bin/perl -w use Image::Size; use LWP::Simple; use Carp; use strict; { my $loc = "http://perlmonks.org/images/usermonkpics/httptech.gif"; my $img = get $loc or croak "Can't get $loc!"; # when the argument passed to imgsize # is a reference (as opposed to a # simple scalar), it assumes it's # pointing to an in-memory buffer, # instead of to a filename. my ($width, $height) = imgsize(\$img); print "Width: $width\n", "Height: $height\n"; }

    [ ar0n ]

Re: Checking Image Size
by joealba (Hermit) on Nov 27, 2001 at 01:01 UTC
    The Image::Magick way:
    use Image::Magick; my $image = Image::Magick->new(); my $x = $image->ReadImage($filename); if ($x) { die "Problem reading image file: $x\n"; } my ($height, $width) = $image->Get('height','width');
Re: Checking Image Size
by belg4mit (Prior) on Nov 27, 2001 at 01:13 UTC
    Image::Info/Image::Info also allows this and much more meta-information retreival as well as many more formats than Image::Size (IIRC).

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: Checking Image Size
by tomhukins (Curate) on Nov 27, 2001 at 00:10 UTC