I’m working on a side-project right now that is using Gravatars and found the wonderful article by Ryan Lanciaux on creating an HtmlHelper extension.  His extension was good, but did not use integrate with the FluentHtml model of MvcContrib, so I refactored his original into the following class, which does the same thing, but allows for fluent building of all the options.  Someone will undoubtedly point out that this could have used a couple of Enumerations, and their right, but I decided the API was static enough that I’d just create the methods. Anyway, I hope someone else gets some use out of this.

using System.Collections.Generic;
using System.Security.Cryptography;
using System.Web.Mvc;
using System.Web.Routing;
using MvcContrib.FluentHtml.Elements;
using System;
using System.Web;

public class Gravatar : Element
{
    private string _email;
    private string _default;
    private string _rating;
    private int _size;
    private string _alt;

    public Gravatar(string email) : base("img")
    {
        _email = email;
    }

    public Gravatar DefaultToUrl(string url)
    {
        _default = url;
        return this;
    }

    public Gravatar DefaultToIdenticon()
    {
        _default = "identicon";
        return this;
    }

    public Gravatar DefaultToMonsterId()
    {
        _default = "monsterid";
        return this;
    }

    public Gravatar DefaultToWavatar()
    {
        _default = "wavatar";
        return this;
    }

    public Gravatar DefaultTo404()
    {
        _default = "404";
        return this;
    }

    public Gravatar Size(int size)
    {
        if (size < 1 || size > 512) throw new ArgumentException("Gravatars can only be between 1 and 512 in size.", "size");
        _size = size;
        return this;
    }

    public Gravatar GRated()
    {
        _rating = "g";
        return this;
    }

    public Gravatar PGRated()
    {
        _rating = "pg";
        return this;
    }

    public Gravatar RRated()
    {
        _rating = "r";
        return this;
    }

    public Gravatar XRated()
    {
        _rating = "x";
        return this;
    }

    public Gravatar AlternateText(string alt)
    {
        _alt = alt;
        return this;
    }

    protected override TagRenderMode TagRenderMode
    {
        get
        {
            return TagRenderMode.SelfClosing;
        }
    }

    public override string ToString()
    {
        var src = string.Format("http://www.gravatar.com/avatar/{0}?", EncryptMD5(_email));

        if (!String.IsNullOrEmpty(_rating)) src += string.Format("r={0}&", HttpUtility.UrlEncode(_rating));
        if (_size != 0) src += string.Format("s={0}&", _size);
        if (!String.IsNullOrEmpty(_default)) src += string.Format("d={0}&", HttpUtility.UrlEncode(_default));

        base.builder.MergeAttribute("src", src);
        base.Attr("alt", _alt ?? "Gravatar");

        return base.ToString();
    }

    private static string EncryptMD5(string Value)
    {
        using(var md5 = new MD5CryptoServiceProvider())
        {
            byte[] valueArray = System.Text.Encoding.ASCII.GetBytes(Value);
            valueArray = md5.ComputeHash(valueArray);
            string encrypted = "";
            for (int i = 0; i < valueArray.Length; i++)
                encrypted += valueArray[i].ToString("x2").ToLower();
            return encrypted;
        }
    }
}

public static class GravatarHtmlHelper
{
    public static Gravatar Gravatar(this HtmlHelper html, string email)
    {
        return new Gravatar(email);
    }
}