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

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!!!!

Comment on Grabbing Specific Image
Download Code
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 http://www.dimages.com/image_sample.jpg you need to do something about URL dispatch there, because all http://www.dimages.com/* 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://};
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 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("http://www.dimages.example.com/image_sample.gif", "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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://613004]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-10-22 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (122 votes), past polls