I have a webpage that I want to dynamically display content depending on the path of the page. For example, I may want http://www.page.php?input=red
or maybe I'll want http://www.page.php?input=blue
. In both cases, the actual content is located in ./$dir/
.
I've got the gist of this down, but I can't for the life of me figure out images. I have an image in each directory at ./$dir/subdir/images/sample.png
. How do I output HTML that dynamically alters the image element?
Here is sample code to demonstrate my question.
<html>
<?php
$dir = $_GET["input"];
$img = imagecreatefrompng("./$dir/subdir/images/sample.png");
?>
<body>
<img src="???" alt="sample image" />
</body>
</html>
I want to display ./red/subdir/images/sample.png
when I have input=red
, ./blue/subdir/images/sample.png
when input=blue
, etc.
Many thanks in advance for any help.
Edit:
I don't even know if I can store an image into a variable as seen on line 4. I'm really new to PHP.
If only one image:
<img src="<?= $img ?>" alt="sample image" />
If loads of images, use an array.
I would also set the alt text on a database, pull it from there and write it the same way I suggested writing the src.
Just so you can understand:
<?= $img ?>
Does exactly the same thing as
<?php echo $img; ?>
Except with a bit more class.
edit: Typo on the indent made my code disappear.
if (!empty($_GET['input']) {
$path = './'.$dir.'/subdir/images/sample.png';
if (file_exists($path)) {
print '<img src="'.$path.'" alt="sample image" />';
}
}