Playing with Gravatar images

by Mads Kristensen 31. March 2008 05:48

Gravatar is a service that associates your e-mail addresses with a picture of you for use on blog comments, forums and bulletin boards. I’ve been a happy user for years.

A cool thing about Gravatar images is that they can be used from any application either on a website or a desktop application. But still, they are primarily used on blogs and forums.

Today, I played with the thought of a wider use and wrote some code that makes working with Gravatars very easy on both web and the desktop. The reason for this is that I needed a way to collect Gravatar images for a list of users, but only the users that actually have a Gravatar associated with their e-mail address.

The Gravatar service doesn’t have an API you can use to check if an e-mail address is associated or not. But by downloading a Gravatar image, the response header Last-Modifed is 1970-01-01 if no image is found and the default image is returned. This information can be used to only store the images users have uploaded to the Gravatar service.

An example

The class is called Gravatar and is very simple and small. Here is an example of using the class to download a Gravatar image and save it to disk.

string email = "name@example.com";

Gravatar gravatar = new Gravatar(email);

 

if (!gravatar.IsDefaultImage)

{

  using (FileStream writer = new FileStream("c:\\" + email + ".jpg", FileMode.Create))

  {

    writer.Write(gravatar.Image, 0, gravatar.Image.Length);

  }

}

Download

Download the class below and put it in your App_Code folder. Then you can use the example above from any ASP.NET page or control.

Gravatar.zip (888,00 bytes)

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags:

Server-side

Comments

3/31/2008 6:11:59 PM #

Matt Ellis

If you want a *slightly* more robust means of checking to see if the gravatar is not registered, pass in the default parameter on the url. This is a URL that is 302 redirected to if there is no gravatar. As long as your download code is set to not automatically follow redirects, you can look for 302, check it's the url you passed in and there's your answer.

Cheers
Matt

Matt Ellis United Kingdom |

4/1/2008 3:26:26 AM #

Mike

The Last-Modifed date comes from gravatar in GMT. I had to add .ToUniversalTime to this line so the date would be 1/1/1970 and not 12/31/1969 where I live:

DateTime date = DateTime.Parse(client.ResponseHeaders["Last-Modified"]).ToUniversalTime.Date;

Mike United States |

4/2/2008 12:06:09 PM #

Chris

One thing I don't like about Gravatar is the fact that every time your image is needed a call is made to Gravatar's site.  This means that Gravatar can log all of the places you go on the web that ask for your image.  Pavatar is a more interesting option because it allows you to host the image.  It would be great to have this included in BlogEngine.Net as an additional option to Gravatar...

Chris United States |

4/2/2008 3:12:52 PM #

Mads Kristensen

@Chris,

I totally agree and Pavatars will be supported in BlogEngine.NEXT

Mads Kristensen Denmark |

4/3/2008 12:59:50 AM #

Chris

Sweet.  That's great news.  Thanks Mads.

Chris United States |

4/6/2008 11:22:59 AM #

trackback

Trackback from gOODiDEA.NET

Interesting Finds: 2008.04.06

gOODiDEA.NET |

4/6/2008 11:23:02 AM #

trackback

Trackback from gOODiDEA

Interesting Finds: 2008.04.06

gOODiDEA |

6/20/2008 6:39:04 AM #

Marc Brooks

It's awesome that you remember to lower-case the email before hashing. Most people forget that.

Marc Brooks United States |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008