Your PHP page can return an image:
<?php
$filename = "my_image.png";
$image = fopen($filename, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($filename));
fpassthru($image);
You can then make it read GET argument and return appropriate image. Then you could use it in another pages like this:
<img src="my_image_return_page.php?image=<?=$image_id?>">
This would ofcourse work for PNG image type, for other types you will have to change Content-Type header to appropriate format. You can find available formats here.
Extended answer:
Say you want a script that needs to read image from database according to image id passed.
<?php
//Get image id
$imageId = $_GET['id'];
//You search for your image in database here, and return the location
//Additionally if image with that id wasn't found, you can return location of some image that says "Ooops, we couldn't find image you were looking for".
//Say you save image's location in $imageLocation variable.
//Get pathinfo of your image file
$pathInfo = pathinfo($imageLocation);
$extension = $pathInfo['extension'];
if ($extension == "jpg") {
$contentType = "jpeg";
} elseif ($extension == "png") {
$contentType = "png";
} elseif ($extension == "gif") {
$contentType = "gif";
} else {
//Handle error here if format isn't recognized
}
//Set content type
header("Content-Type: image/".$contentType);
//Set content length
header("Content-Length: ". filesize($imageLocation));
//This is shorter way then using fopen, but has the same result
echo file_get_contents($imageLocation);