Perl grabbing an external image

K

kjhjhjhjadsasda

Hi

Im trying to grab an external image (through url) and save it locally
on the server. Im using image::grab like so:


use Image::Grab;
$pic = new Image::Grab;
$pic->url('http://groups.google.com/groups/img/groups_medium.gif');
$pic->grab;

open(IMAGE, ">$serverpath");
binmode IMAGE;
print IMAGE $pic->image;
close IMAGE;

This saves a file on the $serverpath location but it keeps being
nothing eg 0k. I remember Ive ran into this previously some time.

Any suggestions??..

Thanks!
M
 
U

usenet

John said:
use strict;
use warnings;
...
check if this succeeds

FWIW, I've never used Image::Grab, but I did give a quick try to the
OP's code (but within the ordinary BP guidelines as John suggested).
The $pic object is never populated with data by the grab method. I get
a warning on the print (because it's printing null), but otherwise the
code does exactly what the OP says (ie, not much).

The OP's code is pretty much straight out of the perldocs for the
module (which are crap, BTW - many examples contain syntax or usage
errors).
 
S

Sisyphus

The OP's code is pretty much straight out of the perldocs for the
module (which are crap, BTW - many examples contain syntax or usage
errors).

In that case, I suggest (untested) :

use strict;
use warnings;
use LWP::Simple;
my $url ='http://groups.google.com/groups/img/groups_medium.gif';

my $content = get($url);
open(IMAGE, ">filename.gif") or die "$!";
binmode IMAGE;
print IMAGE $content;
close IMAGE or die "$!";

__END__

Cheers,
Rob
 
J

John Bokma

Sisyphus said:
In that case, I suggest (untested) :

use strict;
use warnings;
use LWP::Simple;
my $url ='http://groups.google.com/groups/img/groups_medium.gif';

my $content = get($url);
open(IMAGE, ">filename.gif") or die "$!";
binmode IMAGE;
print IMAGE $content;
close IMAGE or die "$!";

If you don't use LWP::Simple, you can simply (ha ha) drop the file stuff:

use strict;
use warnings;

use LWP::UserAgent;

my $url = 'http://groups.google.com/groups/img/groups_medium.gif';

my $response = LWP::UserAgent->new->get(

$url,
':content_file' => 'filename.gif'
);

$response->is_success or
die "Download failed: ", $response->status_line, "\n";
 
B

Brian Wakem

Hi

Im trying to grab an external image (through url) and save it locally
on the server. Im using image::grab like so:


use Image::Grab;
$pic = new Image::Grab;
$pic->url('http://groups.google.com/groups/img/groups_medium.gif');
$pic->grab;

open(IMAGE, ">$serverpath");
binmode IMAGE;
print IMAGE $pic->image;
close IMAGE;

This saves a file on the $serverpath location but it keeps being
nothing eg 0k. I remember Ive ran into this previously some time.

Any suggestions??..



I don't know the module so I'll assume your code is ok.

The obvious problem to me is that google block certain useragents, I
don't know what useragent the module declares itself to be, but it's
probably blocked, as is LWP::Simple (libwww-perl/x.xxx).

You'll need to set the useragent to something a browser would use.


$ perl -MLWP::Simple -e 'print length
get("http://groups.google.com/groups/img/groups_medium.gif")'

0

$ perl -MLWP::UserAgent -e 'my $ua = LWP::UserAgent->new( agent =>
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; CS.v0.2; .NET CLR
1.0.3705)" );my
$res=$ua->get("http://groups.google.com/groups/img/groups_medium.gif");print
length $res->content;'

5183
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top