Displaying Gravatars Using C#

March 2, 2009 | By

gravatar

Gravatar is a popular avatar service that links email addresses to the avatar. You do not need to register an account to be able to display gravatars on your site. All you need to do is construct the URL of the image using the user’s email.

To construct the URL concatenate “http://www.gravatar.com/avatar.php?gravatar_id=” to the MD5 hash of the email. This displays an image using the default size (80 by 80 pixels) and the default rating (G). For example the URL of user1@example.com will be:
http://www.gravatar.com/avatar.php?gravatar_id=111d68d06e2d317b5a59c2c6c5bad808

Before calculating the MD5 hash convert all the characters of the email address to lower case. The gravatar is a JPEG image, so if a file extension is required just append to it ‘.jpg’. There are two other parameters that can be set in the URL; s and r. s is the size of the image, it takes a single number that is the size of the width and height of the image. If the value of s is less than 1 or greater than 600 the image returned will be the default size. When the size is added to the example above the URL will look like this:
http://www.gravatar.com/avatar.php?gravatar_id=111d68d06e2d317b5a59c2c6c5bad808&s=60

The r parameter is the MPAA style rating. When users upload an image they are asked to choose the rating of the image. The ratings in order are g, pg, r and x. Passing one of the ratings as a parameter will retrieve the image with this rating or a lower one but not higher. Adding rating to the previous example will make it look like this:
http://www.gravatar.com/avatar.php?gravatar_id=111d68d06e2d317b5a59c2c6c5bad808&s=60&r=g

There are two download links at the end of the post. The class listed below, with overloaded functions of GetURL, and a Windows Forms example that uses the class.

 

Listed at Hot Scripts in ASP.NET

 

Download Class

 


 

Download Source Code

 

 

Filed in: ASP.NET, WinForms | Tags: ,

Comments (5)

Links to this Post

  1. DotNetShoutout | August 31, 2009
  1. I wish I had access to my system. I really want to check this one out. nice one !!

  2. Hey Rakesh, thank you for the comment. And what is wrong with your system?

  3. I tried your link and I tried your app, it seems like it is not working, I always get the same blue avatar with the white G inside ot 🙁

  4. Hi David, thank you for pointing that out. I tried the code with different email addresses. It seems the email addresses you are testing with don’t have images set for all their ratings.