PHP的图像显示问题

问题描述 投票:0回答:1

我目前正试图在Apache(XAMPP)托管的本地网站上使用PHP GD函数显示一个图像,但我的导航器(Google chrome)显示黑屏,用白色方块代替红色字符串。然而,我的导航器(Google chrome)显示的是一个黑色的屏幕,上面有一个白色的方块,而不是红色的字符串。当我删除 "header(...) "行时,它将图像显示为文本。还是来自 "header(...) "行?还是其他原因?

谢谢你了

<!DOCTYPE html>
<html>
 <head>
	<meta http-equiv="Content-type" content="text/html" charset="UTF-8" />
	<title> Title </title>
</head>
<body>

<?php
  header("Content-Type: image/png");

  $im = imagecreatetruecolor(120, 20);
  $text_color = imagecolorallocate($im, 233, 14, 91);
  imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
  
  imagepng($im);

?>

</body>
</html>
php image apache header gd
1个回答
0
投票

请看一下PHP文档 header(): https:/www.php.netmanualenfunction.header.php "请记住,header()必须在任何实际输出之前被调用,无论是普通的HTML标签、文件中的空行,还是PHP的空行。读取带有include,或require,函数,或其他文件访问函数的代码,在调用header()之前,有空格或空行被输出,这是一个非常常见的错误。在使用单个PHPHTML文件时也存在同样的问题。"

在调用之前,不要输出HTML标签(也不要输出其他任何东西)。header()

© www.soinside.com 2019 - 2024. All rights reserved.