Obtaining Gravatar images

Gravatar is a website that, after registering and adding an email, allows you to set an avatar (image) associated to that email, so that sites that query them for gravatar images will return your same image to all of them.

Although Community Server doesn't stores emails of user comments, I wanted to see how the avatars are obtained from Gravatar.com.

The url you have to build is well explained in their website, so I will just place a basic C# code that calculates the MD5 hash and inserts it on the url:

string email = "me@mydomain.com";

System.Security.Cryptography.MD5CryptoServiceProvider CryptoService
[] InputBytes = System.Text.Encoding.ASCII.GetBytes(email.Replace(" ", "").ToLower())

CryptoService = new System.Security.Cryptography.MD5CryptoServiceProvider()

InputBytes = CryptoService.ComputeHash(InputBytes)
emailHash = BitConverter.ToString(InputBytes).Replace("-", "")

gravatarURL = string.Format("http://www.gravatar.com/avatar/{0}.jpg", emailHash);

And we're done, the final URL pointing to a gravatar.

The following image gets one of my email adresses' gravatar (if I change it from the cigar dog one day on my gravatar account, it will change automatically):

There are options (as querystring parameters) as getting the image in a specific resolution, or getting the identicon.

Posted by Kartones on 2009-12-13


Share via: Twitter Linkedin Google+ Facebook