Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my image download with Perl's LWP give me the wrong-sized file?

Tags:

image

perl

lwp

I am trying to get an image from an HTTP server using Perl.

I have the full URL of the file and am attempting to use

my $data = LWP::Simple::get $params{URL};
my $filename = "image.jpg";
open (FH, ">$filename");
print FH $data;
close (FH);

Now, logically, to me at least, this should work. But the files are slightly different sizes, and I can't work out why.

Help!

like image 965
Xetius Avatar asked May 29 '09 15:05

Xetius


2 Answers

You need to use binmode to properly write the image data to disk.

my $data = LWP::Simple::get $params{URL};
my $filename = "image.jpg";
open (FH, ">$filename");
binmode (FH);
print FH $data;
close (FH);

Otherwise it is interpreted as text, and the newlines get munged.

like image 76
dave4420 Avatar answered Sep 21 '22 21:09

dave4420


Dave is right, you should/must set your file handle to binary mode. But you could do all that in one go:

LWP::Simple::getstore( $params{URL}, 'image.jpg' );
like image 42
innaM Avatar answered Sep 23 '22 21:09

innaM