Gravatar’s new features and ASP.NET

by Mads Kristensen 7. July 2008 01:16

Today, I’ve been updating the BlogEngine.NET Gravatar support to include the new fallback types that Gravatar introduced a few months ago.

A Gravatar is an image that is associated with an e-mail address if the owner of the e-mail address has registered with Gravatar.

If an e-mail address doesn’t have an associated image, Gravatar serves a fallback image instead. Before, the fallback image could either be the default blue Gravatar logo or a URL to an image on the web. Now they have introduced three new fallback image types:


MonsterID


Identicons


Wavatars

This is really cool because now we don’t need to implement our own fallback images in BlogEngine.NET or any other web application for that matter. BlogEngine.NET has had its own implementation of the MonsterID and Subtext uses Identicons. Now none of these projects have to maintain that code anymore. I just deleted all the MonsterID images and that freed several megabytes of disk space.

Example

Here is an example on how to use the new Gravatar fallback types in ASP.NET. First we have to create a method that generates the correct Gravatar URL:

/// <summary>

/// Creates a URL to the Gravatar associated with the email address.

/// </summary>

/// <remarks>

/// The fallback parameter can either be a fully qualified URL to a custom

/// image located on the web or it can be "monsterid", "wavatar" or "identicon".

/// In case a null or an empty string is passed as a fallback, the default blue

/// Gravatar image will be shown.

/// </remarks>

/// <param name="email">The email is the key to find the right Gravatar.</param>

/// <param name="size">The size of the returned Gravatar in pixels.</param>

/// <param name="fallback">The fallback if no Gravatar exists.</param>

/// <returns></returns>

public static string Gravatar(string email, int size, string fallback)

{

  if (string.IsNullOrEmpty(email))

    throw new ArgumentNullException("email", "Email must be specified");

 

  if (!string.IsNullOrEmpty(fallback) && fallback.StartsWith("http"))

    fallback = HttpUtility.UrlEncode(fallback);

 

  email = email.ToLowerInvariant().Trim();

  string hash = FormsAuthentication.HashPasswordForStoringInConfigFile(email, "MD5").ToLowerInvariant();

  return "http://www.gravatar.com/avatar/" + hash + ".jpg?s=" + size + "&d=" + fallback;

}

And then call it from an image tag on the web page.

<img src="<%=Gravatar("mail@example.com", 60, "wavatar") %>" alt="Gravatar" />

It's as simple as that.

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

Tags:

ASP.NET

Comments

7/7/2008 7:58:52 PM #

user

A sample comment

user Switzerland |

7/7/2008 7:59:20 PM #

user 2

A sample comment 2

user 2 Switzerland |

7/7/2008 9:04:41 PM #

Blogging Developer

cool.

Blogging Developer Turkey |

7/8/2008 6:40:17 AM #

Juan

Cool, I've implemented it in my blog

Thanks!

Juan Argentina |

7/8/2008 6:33:02 PM #

Cristiano

But if Gravatar servers are offline, the images can not be loaded ...
Why not preserve a mechanism for caching images already downloaded, for example with a httphandler?
This also would improve loading times !

Cristiano Italy |

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