Perl-CGI Image is displayed in browser as binary

2019-07-30 04:20发布

问题:

I was programming a little plugin, which generates an image via Perl GD and displays it in the browser. So far everything works correct, except that the image is displayed in binary character stream. It's rather annoying to google this kind of problem, because I read about temporarily saving the file to the hard disk, which I would love to avoid.

This is the code I have so far:

$image = $print->png; ## the image was created correctly, I was able to 
#view it if I saved it on the local drive

my $cgi = new CGI;

print $cgi -> header(),
  $cgi -> start_html(),
  $cgi -> h1('Bernd'),
  $cgi -> header(-type=>'image/png');
  binmode STDOUT;
  print $image;
print $cgi -> end_html();

To I have to call a special method of the CGI module? Because I did not find one I am a little bit confused.

Any help is appreciated. Thanks in advance!

回答1:

When it's a PNG, the image must be a separate resource/URL, pointed to by a HTML img element or similar, not flow text embedded into the document. These are the basics of HTML, get a Web programming book if you are not familiar with that.

Either change your image format to inline SVG, or rewrite the program so that it creates an img element, and another program creating the image HTTP response.

It is possible to combine the original program (HTML response) with the one creating the image response, but I don't recommend this for newcomers.



标签: perl cgi