Synchronized Caching of Images, Apache Safe
I noticed when abandoning the sexy 1 byte unicode dingbats for 160 byte PNGs in femtoblogger (to support the wretched Windows users of this world) that my images were being transfered on each page load. Bad for load time.
I had wrongly assumed that the Apache expiration module would give them
a short expiration time by default. No problem, I added the expiration
specification to my .htaccess
file and managed to break my files
completely. The system-wide Override setting prohibit that.
I could make server-wide changes, but with an eye toward users that can’t I came up with a new, better solution.
I serve the images through PHP. That is about half the speed of native, but I can put on a proper Expires header. (And PHP doesn’t put on its don’t cache me ever headers if you have a image/png content type.)
That gets more portable and safer, but how about better? I set my images so they all expire out of cache at the same time. Now I won’t have odd windows where some images have changed and others haven’t. When one icon expires, they all expire. It goes like this…
<php?
$icon = $HTTP_GET_VARS['icon'];$icons = array( 'edit' => 'images/edit.png',
'delete' => 'images/delete.png');Header('Content-type: image/png');
Header('Expires: ' . gmdate('r', strtotime('next Sunday')));fpassthru( fopen( $icons[$icon], 'r'));
?>
Then you can load icon.php?edit and get the PNG for the edit icon with these headers…
HTTP/1.1 200 OK
Date: Wed, 12 Sep 2007 07:23:51 GMT
Server: Apache/2.2.3 (Debian) …
Expires: Sun, 16 Sep 2007 05:00:00 +0000
Content-Length: 164
Content-Type: image/png