steampunk85
steampunk85

Reputation: 23

a php page in img src

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.

Upvotes: 1

Views: 2222

Answers (2)

Ry-
Ry-

Reputation: 224913

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

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

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.

Upvotes: 4

Related Questions