2

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:

  1. Is there something wrong with my code, or does this method always add more time?
  2. How can I speed things up? Is there a safe way to cache the images? I believe Gallery 2 does this somehow.
Jakub
  • 20,418
  • 8
  • 65
  • 92
haudenschilt
  • 160
  • 2
  • 10

5 Answers5

4
  1. Yes your method adds time, by having the image outside of the loop you FORCE PHP to buffer each image into memory, then send it to the user, thereby increasing load on your server and creating a noticeable delay for LARGE images (assuming gallery here).

  2. You can speed this up by having your images in the root of your website/app. To protect your images from remote hotlinking, simply use an htaccess script (google for it). You can also create a combination of htaccess and login permissions to keep un-approved users out of the folders containing the images.

References:

Community
  • 1
  • 1
Jakub
  • 20,418
  • 8
  • 65
  • 92
  • I know how to prevent hotlinking. But that doesn't prevent users from directly typing in the url and seeing the photos. – haudenschilt Apr 12 '12 at 13:53
  • 1
    @haudenschilt, you mean `directory listing` or pulling up a photo directly by knowing its URL? I mentioned using htaccess for validating sessions for users as well (login permissions) – Jakub Apr 12 '12 at 13:59
  • both actually. i'm unaware of how to prevent pulling up a photo directly by url using htaccess. I've looked that up before in the past and couldn't find anything so i assumed it was impossible. – haudenschilt Apr 12 '12 at 14:04
  • 1
    I updated my reference comment to include `protecting directories with htaccess`, that should get you on your way. – Jakub Apr 12 '12 at 14:15
  • Just to be clear... I have my users username/pass info stored in the db. In order to do the .htaccess method i will have to move that info from the db to a .htpasswd file? – haudenschilt Apr 12 '12 at 14:34
  • @haudenschilt, no I supplied references for building a 'bridge' over `htaccess`, its not quick and simple, but it is doable, as is all security. You validate your session from PHP (whatever method you use there) back to htaccess. – Jakub Apr 12 '12 at 14:47
1

Images should be served directly form file rather than by readfile.

Karol Sikora
  • 522
  • 1
  • 4
  • 11
1

In a lot of cases getimagesize(); takes ages. Try to avoid it, and notice if there is any speed difference.

This happens most often with remote images, so not entirely sure you're experiencing the same problem.

Gert Van de Ven
  • 997
  • 6
  • 6
  • I tried removing the getimagesize() and it barely made a difference. Changed from like consistently 600ms to like 550ms. The normal display of those images was more like 100ms or fewer. – haudenschilt Apr 12 '12 at 13:37
  • 1
    Maybe try to echo the pageload time between the several functions and see where it chokes. http://www.phpjabbers.com/measuring-php-page-load-time-php17.html – Gert Van de Ven Apr 12 '12 at 13:45
  • I tried that and the entire script executed in less than a millisecond. – haudenschilt Apr 12 '12 at 14:29
1

Have a look at https://tn123.org/mod_xsendfile/

I've found sendfile to be slightly quicker returning files rather than doing it directly in code.

barryhunter
  • 20,886
  • 3
  • 30
  • 43
-1

Try using jquery plugins or javascript gallery code to display photos instead of php. PHP is a server side language, so it has to contact server to get new images, it may slow down the site.

pandu
  • 21
  • 4
  • You are not answering the OP's question: the question is why the script is slow, not alternative methodes for showing images. – Ivotje50 Jan 07 '14 at 15:52