Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Difficulty with using LWP to display JPEG

by Aquilae (Novice)
on Apr 11, 2014 at 18:16 UTC ( [id://1082006]=perlquestion: print w/replies, xml ) Need Help??

Aquilae has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks.

I am trying to create an HTTP request to an ordinary jpg on a website and return and print it.

Instead of an image, I am seeing what appears to be binary data. I have scoured various online websites to try and remedy this. I figured maybe it's a header issue, or perhaps I need to decode or otherwise modify the binary to some viewable format, but all has been to no avail.

Here is the code:

#!perl use strict; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $server_endpoint = "http://upload.wikimedia.org/wikipedia/en/7/70/B +logscope-logo-simple.jpg"; my $req = HTTP::Request->new(GET => $server_endpoint); $req->header('content-type' => 'image/jpeg'); my $resp = $ua->request($req); if ($resp->is_success) { my $message = $resp->content; print $message; }

I appreciate your time and help with this matter! Thanks!

Replies are listed 'Best First'.
Re: Difficulty with using LWP to display JPEG
by roboticus (Chancellor) on Apr 11, 2014 at 18:20 UTC

    Aquilae:

    That's what I'd expect if I printed a jpeg to the console--a bunch of binary data. Instead of printing it, save it to a file:

    open my $FH, '>', 'image.jpg'; binmode $FH; print $FH $resp->content; close $FH;

    Then use an image viewer to look at the resulting file.

    Update: I just replaced the contents of your if statement with the above lines of code, and tried it. It worked just fine.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thank you for a quick response Roboticus. Let me explain further.

      What I am ultimately trying to do is make several HTTP requests to read in and print multiple images to the browser so that they appear on screen.

      Printing to a file will probably do me no good as I need the image to display in the browser.

        Aquilae:

        LWP lets you talk with servers over the network, it doesn't have anything to do with the browser. If you want the browser to display the images, I'd suggest one of:

        • Use WWW::Mechanize::Firefox to drive a Firefox browser, if you're on a linux machine.
        • Write your program in Javascript, so you can have it load the appropriate objects into a browser window.
        • Create a website where you can direct the browser to the links you want them to see.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        What I am ultimately trying to do is make several HTTP requests to read in and print multiple images to the browser so that they appear on screen.

        How are you calling your script? via an <img> tag in html? If thats the case, before you print the content you need to print the content type. I see this in your code:

        $req->header('content-type' => 'image/jpeg');

        You're setting a header in your lwp request, which is unnecessary. You should replace that line with:

        print "Content-type: image/jpeg\n\n";

        So you can tell the client that requested your script's output the content type of the output it will receive. Although without more information it seems like it would make sense to just have your image pull right from the remote server:

        <img src="http://upload.wikimedia.org/wikipedia/en/7/70/Blogscope-logo-simple.jpg" />

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-19 22:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found