in reply to Difficulty with using LWP to display JPEG

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.

Replies are listed 'Best First'.
Re^2: Difficulty with using LWP to display JPEG
by Aquilae (Novice) on Apr 11, 2014 at 18:26 UTC

    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.

        Roboticus - I am not on a linux machine. Does that mean WWW::Mechanize is going to be useless to me?

        I managed to write some small code but I really do not know which way to go with it. Sadly, I am completely unfamiliar with mechanize so this is what I have come up with:

        use strict; use WWW::Mechanize; my $url = "http://upload.wikimedia.org/wikipedia/en/7/70/Blogscope-log +o-simple.jpg"; #my $cookie_jar; my $mech = WWW::Mechanize->new(); $mech->get($url); $mech->success() or die "Can't fetch the Requested page"; my $val = $mech->content; print $val;

        $val ends up being that same binary nonsense. I took a look at the Image function but it looks like that just scrapes the current page and finds all images and assigns them to an array - I am actually providing a link to the image itself so it doesn't look like that would work.

        Thanks again!

      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" />