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

Here's probably a really easy question for you perl monks to answer, since you guys were so helpful last time. :)

I want to download a jpeg image and save it to a file on my machine. I've currently been creating all the image files I need, but I seem to have no knowledge of how to simply download an existing image from a webpage. For example, if there was a jpeg file at the URL http://www.mywebsite.com/simX/images/coolpic.jpg (there isn't, though), how would I download that and save it to a file on my local machine using a perl script?

-- simX

Replies are listed 'Best First'.
Re: Retrieving Web Images
by liz (Monsignor) on Aug 04, 2003 at 21:36 UTC
      Cool, thanks for the link to the description page. The mirror subroutine for LWP::UserAgent works out perfectly for what I needed to do.

      -- simX
Re: Retrieving Web Images
by tcf22 (Priest) on Aug 04, 2003 at 23:43 UTC
    The easiest way to get and store an image would be to use LWP::Simple. The getstore($url, $file) method should do what you want.
Re: Retrieving Web Images
by fourmi (Scribe) on Aug 05, 2003 at 01:07 UTC
    One of these might get you started..

    #!wperl use LWP::Simple;getstore ' http://url.com/10.jpg, '10.jpg';


    #!perl use strict; require HTTP::Request; require URI::URL; require LWP::UserAgent; sub GetURL; my @Page; @Page = GetURL( "http://localhost/apache_pb.gif"); open (WRITE,">image.gif"); foreach (@Page) { print WRITE $_; } close (WRITE); sub GetURL { my ($inURL) = @_; my ($ua, $request, $response); $ua = new LWP::UserAgent; $request = new HTTP::Request 'GET', $inURL; $response = $ua->request($request); if ($response->is_success) { return $response->content; } else { return $response->error_as_HTML; } }