Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling Images on Webpages

Status
Not open for further replies.

New3Perl

Programmer
Sep 7, 2009
12
US
I'd like to be able to get a copy of an image that I've seen on a webpage into a image editing/display program. The HTML fragment associated with the image looks like:

<img src='images/theimage.png' border='0'/>

my Perl code to download and save the image looks like:

my $browser = LWP::UserAgent->new();
my $response;
$browser->env_proxy();
$browser->cookie_jar($cookie_jar);

my $Image = '
$response = $browser->get($Image);

my $FileName = 'C:\Perl\Image.png';
open "CONTENT", ">", $FileName;
print CONTENT $response->content();
close CONTENT;

This seems to be working ok in the sense that the code produces an output file that looks like it has a bunch of binary stuff in it that starts with a character followed by 'PNG'. But I am unable to open the file with my picture viewer. Is there some sort of header that needs to be removed so that the image file is valid?

A second question: Another part of the HTML that I am looking at looks like the following:

<img id="image2" src="../image2.asp" />

The extension on this picture doesn't look familiar to me (it's not .gif, .png, etc.). How can I tell how to interpret the response to getting this file?
 
Sounds like the second one is being served by ASP, possibly from a database. But it should be OK by the time you get the data, as regardless of how it is served it should still look like a file when it arrives at your 'browser'. You will just have to figure out what format it is and use an appropriate extension when you save it.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Any ideas of how to figure out what kind of image file it is?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top