Gravatar Personal Server

Software designed to combat the problem of slow-loading Gravatar images from the Gravatar server.

Gravatar Personal Server is a piece of software I wrote several months ago designed to combat the problem of slow-loading Gravatar images from the struggling Gravatar server. The software is essentially a caching system that stores Gravatar images on your server for later retrieval. In essence this turns your server into a miniature personal Gravatar server, so should gravatar.com become sluggish in operation (as was the case in the latter half of 2006) then your site will not suffer so badly.

The process of how the software works can be seen to the left.

Diagram of the Gravatar Personal Server operational process

Diagram of the Gravatar Personal Server operational process
[click to enlarge]

Requirements

The Gravatar PS engine is written in the open source scripting language PHP. Additionally it also utilises features of the GD Graphics Library. Both resources are mandatory but are also very popular on Linux servers, so if you're running one then it's pretty likely you'll be good to go.

Software Setup

The software engine is built as a class, and is hence very easy to setup and use, even if you possess very little knowledge of PHP. First upload gravatarPS.php (free download available at the bottom of the page) to an appropriate directory on your web server. Create a new PHP file called something like 'render_gravatar.php' - this is your instantiation script. It does not matter where this file is located in relation to the main engine, so long as you don't store it in a protected directory. Before the software will run correctly you'll need to provide a location to store cached Gravatar images; create a new directory on your filesystem and make sure you give it #777 access permissions so that the caching process is not hindered by any server read/write permissions.

Next open the 'render_gravatar.php' file you created and paste into it the following code:

<?php

//-----------------------------EDIT THESE--------------------------------------------------------------------------
require_once('includes/gravatarPS.php'); // The local location of the Gravatar PS Engine, in relation to this file
$cache_directory = 'media/gravatars/';   // The relative location of your cache directory, with trailing slash '/'
$refresh_period = 604800;                // The lifespan of your cached Gravatar images in seconds. Default 1 week.
//-----------------------------------------------------------------------------------------------------------------

$gravatar = new gravatarPS($_GET,$cache_directory,$refresh_period);
$gravatar->render();
?>

With the area I've identified for editing you should be setting the following variables:

  1. The local filepath of the Gravatar PS engine.
  2. The local filepath of the cache directory you created.
  3. The refresh period in seconds. This is an optional variable, see below for details.

The last variable you can set is the refresh period. This identifies the lifespan of cached Gravatar images before expiry. The length of this time period is entirely up to you; long time periods can lead to out-of-date Gravatar images but less reliance on gravatar.com server. To the contrary a short lifespan will mean your cached gravatars are regularly updated, but will require much heavier reliance on the Gravatar server. By default I've given the software a cache expiry of one week (604800 seconds).

Integration

Now you've set up the Gravatar engine on your server all there's left to do is integrate it into your site's comments. This process is just about identical to connecting directly with gravatar.com. Where as with the latter you may set up a link like this:

http://www.gravatar.com/avatar.php?gravatar_id=53c24b66ce4df799cb8bd1f4ee43bd1d&size=32&default=http://www.stevetucker.co.uk/default_gravatar.gif

with Gravatar PS you simply direct the very same query (everything after the question mark) to the 'render_gravatar.php' instantiation page you created on your server:

/render_gravatar.php?gravatar_id=53c24b66ce4df799cb8bd1f4ee43bd1d&size=32&default=http://www.stevetucker.co.uk/default_gravatar.gif

And that's it. Gravatar PS will kick into action and manage all of your Gravatar requests automatically, caching images loaded from gravatar.com and relocating them on next call. Using the software should provide you with notably faster page loading times, particularly when the main gravatar.com server lags. On a larger scale if enough people adopt this system then it will radically reduce the weight put on the main gravatar.com server and benefit everyone.

I've released the Gravatar PS source code under the Creative Commons Attribution-Share Alike 3.0 License. You can download a copy if it below.

Download IconDownload Gravatar PS .zip (v1.1)

Dated: 31/03/2007. Filed under: Projects and Web Design.


Journal Archive

Search the archives of this website for design and web-related articles.

Search Archive:

Miscellaneous

Other fluff that doesn't fit within any other category.

XHTML | CSS | 508