开发者

What is an efficient way to serve images (PHP)?

Hey guys currently I am thinking of serving images using an image handler script. I have two sources of images. One is from my web images folder where images that are used to construct my site interface are served. The other is in each users images folder where they can store their own images. I was thinking of giving each user image a unique id and then searching that id with the image handler script and serving the image, and changing the file name. The problem is that my site images folder does not have any information in the database and thus has no ids, should I just serve directly? Also this way of serving user images does not seem like the most efficient. If anyone has any suggestions I would really appre开发者_如何学Gociate it.

$sql="SELECT username,file_name FROM images WHERE id=?";
$stmt=$conn->prepare($sql);
$result=$stmt->execute(array($ID));

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$image_name = $row['file_name'];
$username = $row['username'];
}

$path="$username/images/$image_name";


header("Expires: -1");

header("Cache-Control: no-store, no-cache, must-revalidate");

header("Cache-Control: post-check=0, pre-check=0", false);

readfile($path);


The most efficient way to serve images is to have them all on a separate static-only cookie-free domain. This is how sites like Flickr work. All you need to store in the database is enough info to construct the static URL.


The most efficient way to serve images is to not use PHP at all.

Let your web server serve images directly from disk. They end up cached in memory by Linux anyway, so it's really fast. Several times faster than anything you can possibly do in PHP. Load up firebug and try it yourself. Watch how long it takes for apache to return an image from disk, versus from your script (I did a year ago, with a similar script that used readfile()). The difference is significant. If you run a load tester like ab or httperf you'll probably see an even larger difference in requests per second.

There's always X-Sendfile too, which some http servers support.

Alternately, consider putting a caching reverse proxy like varnish in front of your script, so that you're at least


The most obvious efficient way it to forgo PHP and let it be served directly by Apache, with caching. Is there any reason you have to keep track of images in this way you can't do with just parsing access_log's after the fact? And why no caching, I assume a new image gets a new unique id, not a rehashed old one?

That being said: if you do need database retrieval & serve it with php, it is about as efficient as you can get, although be very war of folders named by usernames, a possible security issue. Maybe cache the query result somewhere in memcached or the like, that's about it.


I'm not sure I understand what your problem is but, in your templates, instead of:

<img src="image_handler.php?id=123" />

you should write:

<?php
    $file_data = get_file_data(123);
?>
<img src="<?php echo $file_data['user_name'], "/images/", $file_data['image_name']; ?>" />

And don't forget to escape your data.

You should not serve your files with PHP. Moreover, you should find a webhost that offers you Apache and lighttpd or nginx. Apache is not very fast when it comes to serving static files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜