Perl-CGI Image is displayed in browser as binary

我怕爱的太早我们不能终老 提交于 2019-12-11 08:15:11

问题


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.



来源:https://stackoverflow.com/questions/17964806/perl-cgi-image-is-displayed-in-browser-as-binary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!