So I have a site that requires login to view, which has a photo gallery. In order to protect those photos I've moved them outside the web root and use a php script to display them. Something like this.
photogallery.php
echo '<img src="photo.php?id=X"/>';
photo.php
$id = (int)$_GET['id'];
$photo = getPhotoDataFromDb($id);
$path = DIRECTORY.basename($photo['filename']);
// Make sure photo file exists
if (!file_exists($path) || !is_file($path))
{
logError(__FILE__.' ['.__LINE__.'] No photo found in directory ['.$path.'] for photo id ['.$id.'].');
header('HTTP/1.0 404 Not Found');
return;
}
$info = getimagesize($path);
header("Cache-control: public, no-cache;");
header("Content-type: ".$info['mime']);
readfile($path);
Everything works fine except that this is 6 to 7 times slower than just displaying the photos normally from a directory inside the web root.
So, my questions are:
- Is there something wrong with my code, or does this method always add more time?
- How can I speed things up? Is there a safe way to cache the images? I believe Gallery 2 does this somehow.