简体   繁体   English

header('内容类型:图片/png'); 使浏览器全黑

[英]header('Content-type: image/png'); makes the browser all black

This a script from PHP.net这是来自 PHP.net 的脚本

// Create the image handle, set the background to white
$im = imagecreatetruecolor(100, 100);
imagefilledrectangle($im, 0, 0, 100, 100, imagecolorallocate($im, 255, 255, 255));    
// Draw an ellipse to fill with a black border
imageellipse($im, 50, 50, 50, 50, imagecolorallocate($im, 0, 0, 0));
// Set the border and fill colors
$border = imagecolorallocate($im, 0, 0, 0);
$fill = imagecolorallocate($im, 255, 0, 0);
// Fill the selection
imagefilltoborder($im, 50, 50, $border, $fill);
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

The line HEADER make the browser all black!一行 HEADER 使浏览器全黑! Why?为什么? Whats wrong?怎么了?

header('Content-type: image/png');

Forces your browser to interpret your output as an image of type png.强制您的浏览器将您的输出解释为 png 类型的图像。

Even though your code is fine and does not produce an error.即使您的代码很好并且不会产生错误。 It's very likely that you are missing GD or your environment is badly configured and do return an error.您很可能缺少 GD 或您的环境配置不当并返回错误。 But because of the header , the error output is behing interpreted as an image.但是由于header ,错误输出被解释为图像。

Try removing it and ensure that the displayed string is a correct raw png data.尝试删除它并确保显示的字符串是正确的原始 png 数据。

Edit: After debugging with OP, it came out that his script had a whitespace character before declaring open php tags <?php .编辑:使用 OP 进行调试后,发现他的脚本在声明打开的 php 标签<?php之前有一个空格字符。 This small thing was sent as well with the correct png data and messed up the whole data for the browser interpreting it.这个小东西也与正确的 png 数据一起发送,并弄乱了浏览器解释它的整个数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM