in reply to Re^3: showing an image downloaded with LWP::Simple
in thread showing an image downloaded with LWP::Simple

Thanks again for your assistance. Here is the code below:

#!/usr/local/bin/perl #Warn browser that HTML is coming print "Content-type: text/html\n\n"; print header; print STDOUT "<html>\n<head>\n"; print STDOUT "<title></title>\n"; print STDOUT "</head>\n"; print STDOUT "<body>\n"; use LWP::Simple; print $content = get("http://www.nhc.noaa.gov/storm_graphics/AT06/refr +esh/AL0606W5+gif/144631W_sm.gif"); print STDOUT "</body>\n"; print STDOUT "</html>\n";

Basically, I just want to display the image when I go to the link.

Edit: g0n - code tags

Replies are listed 'Best First'.
Re^5: showing an image downloaded with LWP::Simple
by Joost (Canon) on Sep 08, 2006 at 18:52 UTC
    A couple of points:
    • ugly hacks aside, you can't just dump image data inside HTML, even if you do it from a script. You should use img (or object) tags.
    • There is no link in your HTML so I don't know what link you're talking about.
    • If you want to show a remote image, what's wrong with <img src="http://www.nhc.noaa.gov/storm_graphics/AT06/refresh/AL0606W5+gif/144631W_sm.gif"> ?
    • Please read writeup formatting tips - or at least use <code> tags around your code.
    • If you really really want to, use something like this: (in your html): <img src="/path/to/script.cgi"> and (in your script.cgi):
      #!/usr/bin/perl -w use strict; use LWP::Simple; print "Content-type: image/gif\n\n"; binmode STDOUT; getprint "http://www.nhc.noaa.gov/storm_graphics/AT06/refresh/AL0606W5 ++gif/144631W_sm.gif";
      Thank you! Your explanation really helped!

        I use the following html to display an image via a perl script:

        <img src="randimage.cgi" border="2" alt="[mini-beast]">

        Where randimage.cgi is:

        #!/usr/bin/perl -wT use strict; my @files = glob("./jpegs/*.jpg"); print "Location: $files[int(rand @files)]\n\n";

        If that's any help...

        ...and, no, I don't know what the 'Location: ' and two newlines are for - I can only assume that they are normally output by a web-server as part of the protocol...

        ... okay, looked 'Location: ' up - it's a redirection command, which makes sense.

        Tom Melly, tom@tomandlu.co.uk
Re^5: showing an image downloaded with LWP::Simple
by RobPayne (Chaplain) on Sep 09, 2006 at 14:33 UTC
    You haven't told the browser that there is an image coming. The only MIME type that you have given is "text/html", you need to drop all of the html and tell the browser to expect "image/gif". Take a look at the Downloading a file discussion, where there is discussion of headers similar to what you need to set.
    Hint: You should take a look at the headers that you get back when you download the image from www.nhc.noaa.gov, and probably hand them back, or directly link to the noaa image from your existing html, like this:
    print STDOUT "<html>\n<head>\n"; print STDOUT "<title></title>\n"; print STDOUT "</head>\n"; print STDOUT "<body>\n"; print STDOUT "<img src=\"http://www.nhc.noaa.gov/storm_graphics/AT06/r +efresh/AL0606W5+gif/144631W_sm.gif\"></img>\n"; print STDOUT "</body>\n"; print STDOUT "</html>\n";
    Update: see the responses, above, from Joost and Melly.