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

Hi, folks:

With GD::Image, I can check the width and height of a local image file by the following code

my $image = GD::Image->new('/path/to/local/file.jpg'); my $width = $image->width; my $height = $image->height;

How can I do the same on a remote image like 'http://example.com/images/file.jpg'.

Many thanks

lihao

Replies are listed 'Best First'.
Re: Question: how to check remote image property
by ikegami (Patriarch) on Apr 07, 2008 at 19:53 UTC
    use LWP::Simple qw( get ); my $image = get("http://example.com/images/file.jpg"); defined($image) or die("Unable to fetch image\n"); my $image = GD::Image->new($image);
      Looks right to me, but beware that this loads the whole image into memory. If this URL is being supplied by untrusted users you'll need to do something to prevent being crashed by someone pointing you to a gigantic image. Even trusted users can do this - I once had users uploading 200MB+ uncompressed images into Krang so they could include them on web-pages (shrunk via HTML width/height attributes, of course!).

      Anybody know how you tell LWP to stop if it gets to X bytes? Some kind of read-callback, yes?

      -sam

        Can't do it with LWP::Simple (natch), but yes if you use $ua->get( $url, :content_cb => ... ) you can pass a callback which dies after the requisite number of bytes have been received.

        #!/opt/local/bin/perl use strict; use LWP::UserAgent (); my $ua = LWP::UserAgent->new(); $ua->env_proxy; my $rcvd = 0; my $buf = ''; my $resp = $ua->get( 'http://www.google.com/', ':content_cb' => sub { $buf .= $_[0]; die "NO MOR +\n" if ( $rcvd += length $_[0] ) > 512; }); print "content:\n", $buf, "\n\n"; print "headers:\n", $resp->as_string, "\n"; exit 0; __END__

        If the callback gets too much your response object will have an X-Died header (if you care if it did or not).

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: Question: how to check remote image property
by cosmicperl (Chaplain) on Apr 07, 2008 at 22:32 UTC
    Personally I'd use Image::Size rather than GD to do this.
      But does that work for JPEGs as well as GIFs? I remember writing an image map program in TK and had to physically open a JPEG, and do a bit of unpacking magic on some of the first 9 bytes of the file to get the file dimensions. Granted this was years ago and maybe the module has been updated but as I recall, I tried that and it wouldn't handle the JPEGs.

      Update: Realized the OP was asking about remote images not local.


      Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

      I would love to change the world, but they won't give me the source code

Re: Question: how to check remote image property
by poolpi (Hermit) on Apr 07, 2008 at 20:19 UTC

    You can have a look at WWW::Mechanize and $mech->find_image() who returns a WWW::Mechanize::Image object

    Update: #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; use Data::Dumper; my $url = q{http://perlmonks.org/}; my $m = WWW::Mechanize->new(); $m->get($url); print Dumper $_ for @{ $m->images };
    Output: $VAR1 = bless( { 'width' => '468', 'base' => bless( do{\(my $o = 'http://perlmonks.org/' +)}, 'URI::http' ), 'alt' => 'Beefy Boxes and Bandwidth Generously Provid +ed by pair Networks', 'name' => undef, 'url' => 'http://promote.pair.com/i/pair-banner-curre +nt.gif', 'tag' => 'img', 'height' => '60' }, 'WWW::Mechanize::Image' ); $VAR1 = bless( { 'width' => '74', 'base' => bless( do{\(my $o = 'http://perlmonks.org/' +)}, 'URI::http' ), 'alt' => 'Ovid', 'name' => undef, 'url' => 'http://perlmonks.org/images/usermonkpics/ov +idmonk.gif', 'tag' => 'img', 'height' => '91' }, 'WWW::Mechanize::Image' );

    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

      It's important to distinguish that as an HTML attribute for displaying the image and not as a property of the image itself. Have to have the image (its data) to read that.

Re: Question: how to check remote image property
by ack (Deacon) on Apr 08, 2008 at 05:59 UTC

    I'm not clear on your objective.

    Are you trying to avoid downloading the image (i.e., do want to get to its attributes without...or before you...download it?)

    Or is it OK to download it and then get the attributes?

    ack Albuquerque, NM