Create Gravatar Photo Urls in Javascript

Sometimes one needs quick profile images in a development project. Enter Gravatar:

Your Gravatar is an image that follows you from site to site appearing beside your name when you do things like comment or post on a blog. Avatars help identify your posts on blogs and web forums, so why not on any site? source: http://en.gravatar.com/

The only meaningful step in generating image urls is creating an MD5 hash of the users email, which is pretty easy to do in Javascript (i.e. Ember.js & Phonegap projects).

1
2
3
  var loweredEmail = email.trim().toLowerCase();
  var md5 = MD5(loweredEmail);
  var photoUrl = "http://www.gravatar.com/avatar/" + md5;

If you need a sample MD5 function, take a look at this repo.

You should spend some time getting to know what paramaters you can pass to Gravatar, such as size and default images here.