页眉(';内容类型:image/png';);使浏览器全部变黑


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

这是来自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);

HEADER行使浏览器变黑!为什么?怎么了?

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

强制浏览器将输出解释为png类型的图像。

即使您的代码很好并且不会产生错误。很可能您缺少GD,或者您的环境配置不正确,并返回错误。但由于header,错误输出被解释为图像。

请尝试删除它,并确保显示的字符串是正确的原始png数据。

编辑:在使用OP进行调试后,发现他的脚本在声明openphp标记<?php之前有一个空白字符。这个小东西也被发送了正确的png数据,并将整个数据搞砸,以便浏览器进行解释。