Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Grabbing Specific Image

by Anonymous Monk
on May 01, 2007 at 15:50 UTC ( #613004=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

All I am trying to do is to go to an specific image url and save a copy of it to a local directory, and my code doesn't work. Is there a easier way of doing that? Here is some of my code, may be someone could show me where I am doing something wrong.

... use LWP::Simple; use HTML::TokeParser; use CGI::Carp; my ($image, $image_dir, $image_url, $new_image); print "Content-type: text/html\n\n"; $image_dir = "../../store_here"; # $image should contain GIF data after this. $image_url = "http://www.dimages/image_sample.gif" unless $url =~ m{^h +ttp://}; $image = get($image_url) || ''; ###### # Save copy of image open(OUTPUT_IMG_FILE, ">$image_dir") || die "Unable to open $image_dir +: $!"; binmode(OUTPUT_IMG_FILE); print OUTPUT_IMG_FILE $image; close(OUTPUT_IMG_FILE); ######

Thanks a lot!!!!

Replies are listed 'Best First'.
Re: Grabbing Specific Image
by merlyn (Sage) on May 01, 2007 at 16:46 UTC
    People work too hard at copying URLs to disk. Witness:
    use LWP::Simple qw(mirror); mirror("", "local_file. +gif");
    What's nice is that this also respects being re-run, caching the local version and properly passing "if-modified-since" to minimize the work if the file hasn't changed.
      That works, but how could I send the image file to another directory.
Re: Grabbing Specific Image
by Joost (Canon) on May 01, 2007 at 16:03 UTC
Re: Grabbing Specific Image
by Albannach (Prior) on May 01, 2007 at 16:07 UTC
    This is easier: perl -MLWP::Simple -e"getstore $url, $localfile"

    I'd like to be able to assign to an luser

Re: Grabbing Specific Image
by akho (Hermit) on May 01, 2007 at 16:03 UTC
    There is a misprint in the URL; and you're trying to open a direcrory for writing.

    The line where you're setting $image_url is also suspicious; what's $url?

    Upd And if you actually meant you need to do something about URL dispatch there, because all* URLs seem to return the same page. Also, add <body> tags to that page (it seems to be a work in progress, and I boldly assume it's yours; excuse me if it is not so).

      I am sorry, it should be
      $image_url = "http://www.dimages/image_sample.gif" unless $image_url = +~ m{^http://};

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://613004]
Approved by Joost
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2017-04-25 06:15 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (449 votes). Check out past polls.