in reply to Re: Question: how to check remote image property
in thread Question: how to check remote image property

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

  • Comment on Re^2: Question: how to check remote image property

Replies are listed 'Best First'.
Re^3: Question: how to check remote image property
by Fletch (Bishop) on Apr 07, 2008 at 21:20 UTC

    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.