I’ve been playing around with different ways to use Gravatars recently. The basic concept of Gravatar is very simple - you upload an avatar to an account that is identified by your email address. The avatar can then be downloaded by anyone, anywhere, anytime as long as they have your email address. This allows any type of software that knows people’s email addresses to add a human touch the visual presentation of anything related to that person. Gravatar’s widest usage base is currently in blog comments, and most of the major blogging platforms already have native Gravatar support. The comments on this blog are Gravatar enabled - if you are wondering how to get a custom picture to show up in the comments section below, head over to the Gravatar website and set up a free account.
Accessing the Gravatar API
Using the Gravatar API (if you want to call it that) is very simple. The following URL will give you a JPEG image of my Gravatar.
The jumple of numbers and letters at the end of the URL is an MD5 hash of my email address. Luckily for us, OS X 10.5 comes with
md5, a command line utility for calculating MD5 hashes of files. To calculate the hash of a string, you can simply
echo the string to
stdout, then pipe that into
md5. To calculate the MD5 hash for “firstname.lastname@example.org”, you can use the following:
echo -n "email@example.com" | md5
A few quick things to note here:
- When calculating the MD5 hash for the Gravatar API, you have to use all lower-case letters in the email address.
echodisables the trailing new-line character that
echonormally spits out.
Download It Using curl
Now we can construct the Gravatar URL, and use
curl to download it:
curl http://www.gravatar.com/avatar/`echo -n firstname.lastname@example.org | md5` > gravatar.jpg
That’s all there is to it. When you run this command, a file named “gravatar.jpg” will be generated in your current directory that contains the Gravatar for “email@example.com”. Substitute your own (or someone else’s) email address, and you’ll get the corresponding Gravatar. You can also request the Gravatar to be a certain size, or change the default Gravatar returned for an unknown email address by modifying the URL query as described here.