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

Hi guys,

I'm really stuck on this one. I need to download a whole lot of pictures from the web. But the good thing is that I only need the first 200 bytes of each image. Is there any way for me to only download those 200 bytes, without having to download the full image ?
Thanks Monks :)

//Preben
  • Comment on How to only download first 200 bytes of an image ?

Replies are listed 'Best First'.
Re: How to only download first 200 bytes of an image ?
by samtregar (Abbot) on Sep 13, 2007 at 20:19 UTC
    That depends on the server. If the server supports it you can pass a Range header requesting just the first 200 bytes. The server may decide to just send you the whole thing anyway. If you're willing to write your own HTTP client you could have it close the connection after it gets enough of the image.

    -sam

Re: How to only download first 200 bytes of an image ?
by ikegami (Patriarch) on Sep 13, 2007 at 20:22 UTC

    HTTP/1.1 (and maybe earlier) allow you to specify a Range header to specify which parts of the document you wish to download. The server may ignore the Range request and return the entire document. In that event, you could simply close the connection once you've received enough bytes.

      Note that if the image is small enough, you may be better off to just receive the whole image anyway, since closing and re-opening a connection may be slower than receiving a few hundred extra bytes (many modern HTTP implementations keep the connection open for consecutive requests by default, though I'm not sure if LWP::UserAgent does).

      To OP: check the content-length header.

Re: How to only download first 200 bytes of an image ?
by blah (Novice) on Sep 13, 2007 at 20:53 UTC
    from WWW::Anonymouse:
    sub _ua_content_cb { my ($data, $res, $proto) = @_; $res->add_content(\$data); # Abort the request if enough content is received to parse for sta +tus. die if length $res->content > MAX_BYTES; } my $res = $self->{ua}->post( $self->_url, \%fields, Referer => $self->_referer, ':read_size_hint' => MAX_BYTES, ':content_cb' => \&_ua_content +_cb, );
Re: How to only download first 200 bytes of an image ?
by Anonymous Monk on Sep 14, 2007 at 00:30 UTC
    my $ua = LWP::UserAgent->new(max_size => 200);