【问题标题】:Image display issue with PHPPHP的图像显示问题
【发布时间】:2020-09-07 02:10:57
【问题描述】:

我目前正在尝试在由 Apache (XAMPP) 托管的本地网站上使用 PHP GD 函数显示图像。但是,我的导航器(谷歌浏览器)显示一个带有白色方块而不是红色字符串的黑屏。当我删除“header(...)”行时,它将图像显示为文本。 错误是否来自 Apache 服务器?还是来自“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】:

    看看header()的PHP文档:
    https://www.php.net/manual/en/function.header.php
    “请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通 HTML 标记、文件中的空白行还是从 PHP 中。使用包含或要求函数读取代码是一个非常常见的错误,或者另一个文件访问函数,并在调用 header() 之前输出空格或空行。使用单个 PHP/HTML 文件时存在同样的问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多