繁体   English   中英

header('内容类型:图片/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 进行调试后,发现他的脚本在声明打开的 php 标签<?php之前有一个空格字符。 这个小东西也与正确的 png 数据一起发送,并弄乱了浏览器解释它的整个数据。

暂无
暂无

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

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