a php page in img src

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

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

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:

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

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

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.

回答1:

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


回答2:

Instead of using an image tag, use this:

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

When using an image tag, the source must be image data, not another image tag.



转载请标明出处:a php page in img src
文章来源: a php page in img src
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!