You'll need a script like getimage.php
which sends the image headers and echo's out its contents. Then in your HTML, you just utilize it as the <img src=''>
in your HTML. The only purpose of getimage.php
is to retrieve and output the image. It remains separate from whatever PHP you use to generate the HTML sent to the browser.
Additionally, you can check if the user has a valid session and permission to view the image in getimage.php
and if not, send a some kind of access-denied image instead.
The contents of getimage.php
are small and simple:
// Check user permissions if necessary...
// Retrieve your image from $_GET['imgId'] however appropriate to your file structure
// Whatever is necessary to determine the image file path from the imgId parameter.
// Output the image.
$img = file_get_contents("/path/to/image.jpg");
header("Content-type: image/jpeg");
echo($img);
exit();
In your HTML:
<!-- as many as you need -->
<img src='getimage.php?imgId=12345' />
<img src='getimage.php?imgId=23456' />
<img src='getimage.php?imgId=34567' />
It then becomes the browser's job to call getimage.php?imgId=12345
as the path to the image. The browser has no idea it is calling a PHP script, rather than an image in a web accessible directory.