0

I tried the first solution explained in Perl CGI display image to browser from file topic.

Everything works perfect... except for the fact that, instead of displaying the image in the browser, it displays a lot of strange characters (something similar as when I open the image in notepad.

This is the code I am using:

#perl CGI code lines before
select(STDOUT); $| = 1;   #unbuffer STDOUT
print "Content-type: image/jgp\n\n";

open (IMAGE, '<', '/absolute_route_to_image/logo.jpg');
print <IMAGE>;
close IMAGE;
#rest of HTML code, displayed in a print statement in perl

Any help is appreciated. Thank you!!

Community
  • 1
  • 1
  • 3
    show the code that you are using – pcantalupo Nov 01 '15 at 03:46
  • 1
    Most likely you haven't set your Content-Type right. Check out: http://stackoverflow.com/questions/22523718/perl-output-image-from-a-remote-source – Miller Nov 01 '15 at 03:56
  • Thank you for your response! This is the part of my code where I am trying to display the image (excuse me if I put the code wrongly, I recently joined this great community, so still entry level): #some more code lines before select(STDOUT); $| = 1; #unbuffer STDOUT print "Content-type: image/jpg\n\n"; open (IMAGE, '<', '/root_to_image/logo.jpg'); print ; close IMAGE; #After this, I start the construction of the rest of the HTML script from #perl – Danilo Ruano Nov 02 '15 at 10:14

1 Answers1

0

rest of HTML code

An HTTP response can include one resource. You can't spluge out a pile of raw JPEG data in the middle of an HTML document. The browser is processing HTML, not a JPEG.

In general, you need to have the browser make a second HTTP request to get the image (with the URL specified in the src attribute of an <img> element.

It is possible to express the entire resource in the form of a URL using the data: scheme though. The URI::data module can help you generate them.

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

Also, the content type for JPEGs is image/jpeg. Not image/jgp and not image/jpg. You are seeing how browsers treat unknown mime types.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335