img src中的php页面 [英] a php page in img src

查看:156
本文介绍了img src中的php页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a html page that uses a php page to decide what image to display at a set time.

唯一的问题是,当我进入php页面时,它将显示正确的图像,但是当我尝试对php页面进行img src时,它在HTML页面上给出了无效链接.这是我在HTML和PHP页面上使用的代码.

The only problem is when i go to the php page it will display the correct image, but when i try to img src the php page, it gives a dead link on the HTML page. Here is the code I am using on the HTML and PHP page.

HTML:

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

getImage.php:

getImage.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页面,此代码可以很好地显示图像,但是当您将其链接为图像时,它将无法正常工作.我做了一些研究,发现可能需要添加标题:

This code will display the image fine if you go to the PHP page, but when you link it as an image it does not work. I did a little research and found that I might need to add a header:

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

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

but when I add that in i get a dead link and no image is displayed on either the php page or the HTML page. Any help would be greatly appreciated.

推荐答案

这不是图像的工作方式.您需要输出图像的数据,而不是其他HTML片段.

That's not how images work. You need to output the image's data, not another HTML fragment.

<?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);
?>

这篇关于img src中的php页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆