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:
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.
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:
///Creates a URL to the Gravatar associated with the email address.
///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.
///<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>
public static string Gravatar(string email, int size, string fallback)
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("firstname.lastname@example.org", 60, "wavatar") %>" alt="Gravatar" />
It's as simple as that.