1

我正在尝试创建一个 html 页面,该页面使用 php 页面来决定在设定的时间显示什么图像。

唯一的问题是当我转到 php 页面时,它会显示正确的图像,但是当我尝试 img src php 页面时,它会在 HTML 页面上给出一个死链接。这是我在 HTML 和 PHP 页面上使用的代码。

HTML:

<html>
<body>
<img src="http://itcacher85.hostzi.com/getImage.php" />
</body>
</html>

获取图像.php:

<?php
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: January 01, 2013'); // Date in the past
header('Pragma: no-cache');

$h = date('Hi');

if ($h >= 2100 && $h < 2230){ $img = '40px-Dni5.png'; }
elseif ($h >= 2230 && $h < 0000){ $img = '40px-Dni3.png'; }

elseif ($h >= 0000 && $h < 0130){ $img = '40px-Dni7.png'; }
elseif ($$h >= 0130 && $h < 0137){ $img = '40px-Dni6.png'; }

elseif ($h >= 0137 && $h < 0138){ $img = '40px-Dnisolve.png'; }
elseif ($h >= 0138 && $h < 0300){ $img = '40px-Dni6.png'; }

elseif ($h >= 0300 && $h < 0430){ $img = '40px-Dni4.png'; }
elseif ($h >= 0430 && $h < 0600){ $img = '40px-Dni5.png'; }

else{ $img = 'where.png'; }
?>

<img src="<?php echo $img; ?>">

如果您转到 PHP 页面,此代码将很好地显示图像,但是当您将其链接为图像时,它不起作用。我做了一些研究,发现我可能需要添加一个标题:

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

但是当我添加它时,我得到一个死链接,并且 php 页面或 HTML 页面上都没有显示图像。任何帮助将不胜感激。

4

2 回答 2

4

而不是使用图像标签,使用这个:

header("Content-Type: image/png");
readfile($img);

使用图像标签时,源必须是图像数据,而不是另一个图像标签。

于 2012-12-23T16:11:36.467 回答
2

这不是图像的工作方式。您需要输出图像的数据,而不是另一个 HTML 片段。

<?php
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: January 01, 2013'); // Date in the past
header('Pragma: no-cache');
header('Content-Type: image/png');

$h = date('Hi');

if ($h >= 2100 && $h < 2230){ $img = '40px-Dni5.png'; }
elseif ($h >= 2230 && $h < 0000){ $img = '40px-Dni3.png'; }

elseif ($h >= 0000 && $h < 0130){ $img = '40px-Dni7.png'; }
elseif ($h >= 0130 && $h < 0137){ $img = '40px-Dni6.png'; }
# HEY!  ^ LOOK OVER HERE! ... too many $ signs.

elseif ($h >= 0137 && $h < 0138){ $img = '40px-Dnisolve.png'; }
elseif ($h >= 0138 && $h < 0300){ $img = '40px-Dni6.png'; }

elseif ($h >= 0300 && $h < 0430){ $img = '40px-Dni4.png'; }
elseif ($h >= 0430 && $h < 0600){ $img = '40px-Dni5.png'; }

else{ $img = 'where.png'; }

readfile($img);
?>
于 2012-12-23T16:12:06.957 回答