PHP browser image caching

I feel the need, the need for speed!
Posted by Paul Whitrow, 12th June, 2009 | Permalink
Here is a little 'trick' I had forgotten about until I rediscovered it recently, image browser caching. By that I mean your web browser will store images you are viewing on a page, and only reload those images on susequest visits if they have changed.Especially useful then on pages that have dynamic images, for example galleries. Using this technique the browser will only need to load any new images with each visit. The result being a much faster and smoother viewing experience for graphic heavy content.
Web caching is the caching of web documents (e.g., HTML pages, images) in order to reduce bandwidth usage, server load, and perceived lag. A web cache stores copies of documents passing through it; subsequent requests may be satisfied from the cache if certain conditions are met.
The trick is to use PHP's Filemtime function (gets file modification time) to append a timestamp to the filename of the image:
< img src="yourfile.jpg?< ?php echo filemtime('yourfile.jpg')?>" alt="your image" />
How simple is that?
Obviously this only works in PHP pages, but there are other options out there for other file types so Google away.
This technique has now been implemented in xFlow!

Posted on Friday 12th June, 2009 at 8:38 am by Paul Whitrow, and filed under PHP
Wanna keep up to date? Subscribe to the site feeds.
Browse More Entries
Have your say:
NB: Comments are subject to administration.



There are no comments yet for ‘PHP browser image caching’