views:

48

answers:

4

Whilst the Gravatar service's API (well, it's really just a URL) is pretty straightforward, is there a simple helper method out there that does a good job of reflecting all the options available for Gravatar?

  • Image size
  • Default image (when user hasn't specified one)
  • Rating (G/PG/R/X)

Ideally this would be an HtmlHelper extension method.

I'm targetting MVC2 on .NET4, but I suppose others would be interested in options for earlier versions too.

EDIT Implementations should allow providing additional attributes for the generated HTML element too.

+2  A: 

I needed this for a project so I took: http://www.freshclickmedia.com/blog/2008/06/aspnet-gravatar-control-update-full-source-included/

and turned it into this:

public static class GravatarHtmlHelper
{
    public static Gravatar Gravatar( this HtmlHelper htmlHelper, string email )
    {
        return new Gravatar() {Email = email, Size = 50, MaxAllowedRating = WebControls.Gravatar.RatingType.X};
    }

    public static Gravatar Gravatar(this HtmlHelper htmlHelper, string email, bool outputSiteLink)
    {
        return new Gravatar() { Email = email, Size = 50, MaxAllowedRating = WebControls.Gravatar.RatingType.X, OutputGravatarSiteLink = outputSiteLink };
    }

    public static Gravatar Gravatar(this HtmlHelper htmlHelper, string email, short size )
    {
        return new Gravatar() { Email = email, Size = size, MaxAllowedRating = WebControls.Gravatar.RatingType.X };
    }

    public static Gravatar Gravatar(this HtmlHelper htmlHelper, string email, short size, bool outputSiteLink)
    {
        return new Gravatar() { Email = email, Size = size, MaxAllowedRating = WebControls.Gravatar.RatingType.X, OutputGravatarSiteLink = outputSiteLink};
    }

}

public class Gravatar
{
    public enum RatingType { G, PG, R, X }

    private string _email;

    // outut gravatar site link true by default:

    // customise the link title:

    public Gravatar()
    {
        OutputGravatarSiteLink = true;
        LinkTitle = "Get your avatar";
    }

    /// <summary>
    /// The Email for the user
    /// </summary>

    public string Email
    {
        get
        {
            return _email;
        }

        set
        {
            _email = value.ToLower();
        }
    }

    /// <summary>
    /// Size of Gravatar image.  Must be between 1 and 512.
    /// </summary>
    public short Size { get; set; }

    /// <summary>
    /// An optional "rating" parameter may follow with a value of [ G | PG | R | X ] that determines the highest rating (inclusive) that will be returned.
    /// </summary>
    public RatingType MaxAllowedRating { get; set; }

    /// <summary>
    /// Determines whether the image is wrapped in an anchor tag linking to the Gravatar sit
    /// </summary>
    public bool OutputGravatarSiteLink { get; set; }

    /// <summary>
    /// Optional property for link title for gravatar website link
    /// </summary>
    public string LinkTitle { get; set; }

    /// <summary>
    /// An optional "default" parameter may follow that specifies the full, URL encoded URL, protocol included, of a GIF, JPEG, or PNG image that should be returned if either the requested email address has no associated gravatar, or that gravatar has a rating higher than is allowed by the "rating" parameter.
    /// </summary>
    public string DefaultImage { get; set; }

    public override string ToString()
    {

        // if the size property has been specified, ensure it is a short, and in the range 
        // 1..512:
        try
        {
            // if it's not in the allowed range, throw an exception:
            if (Size < 1 || Size > 512)
                throw new ArgumentOutOfRangeException();
        }
        catch
        {
            Size = 80;
        }

        // default the image url:
        string imageUrl = "http://www.gravatar.com/avatar.php?";

        if (!string.IsNullOrEmpty(Email))
        {
            // build up image url, including MD5 hash for supplied email:
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            UTF8Encoding encoder = new UTF8Encoding();
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();

            byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(Email));

            StringBuilder sb = new StringBuilder(hashedBytes.Length * 2);
            for (int i = 0; i < hashedBytes.Length; i++)
            {
                sb.Append(hashedBytes[i].ToString("X2"));
            }

            // output parameters:
            imageUrl += "gravatar_id=" + sb.ToString().ToLower();
            imageUrl += "&rating=" + MaxAllowedRating.ToString();
            imageUrl += "&size=" + Size.ToString();
        }

        // output default parameter if specified
        if (!string.IsNullOrEmpty(DefaultImage))
        {
            imageUrl += "&default=" + HttpUtility.UrlEncode(DefaultImage);
        }



        var linkBuilder = new TagBuilder("a");
        // if we need to output the site link:
        if (OutputGravatarSiteLink)
        {
            linkBuilder.MergeAttribute("href", "http://www.gravatar.com");
            linkBuilder.MergeAttribute("title", LinkTitle);
        }

        // output required attributes/img tag:
        var builder = new TagBuilder("img");
        builder.MergeAttribute("width", Size.ToString());
        builder.MergeAttribute("height", Size.ToString());
        builder.MergeAttribute("src", imageUrl);
        builder.MergeAttribute("alt", "Gravatar");

        string output = builder.ToString(TagRenderMode.Normal);
        // if we need to output the site link:)
        if (OutputGravatarSiteLink)
        {
            linkBuilder.InnerHtml = builder.ToString();
            output = linkBuilder.ToString(TagRenderMode.Normal);
        }

        return output;

    }
}

Usage:

<%= Html.Gravatar("[email protected]", true)%>

You'll have to add the overloads to the html helper as you need them. I can't do all the work. ;) Not bad for 5 min eh?

jfar
Thanks for this. If only I was more patient -- I already implemented my own solution (posted as an answer)... One comment, the email address should be trimmed before being hashed to guarantee a numeric match. The URL format has been updated a little since this implementation was written, though I suppose it doesn't matter as I'm sure they're backwards compatible.
Drew Noakes
A: 

Rob Conery's gravatar helper extension linked below would be a good start.

My Favorite Helpers For ASP.NET MVC

The code from the site:

public static class GravatarHelper {

    public static string Gravatar(this HtmlHelper helper, string email, int size) {
        var result = "<img src=\"{0}\" alt=\"Gravatar\" class=\"gravatar\" />";
        var url = GetGravatarURL(email, size);
        return string.Format(result, url);
    }

    static string GetGravatarURL(string email, int size) {
        return (string.Format("http://www.gravatar.com/avatar/{0}?s={1}&amp;r=PG", 
                    EncryptMD5(email), size.ToString()));
    }

    static string GetGravatarURL(string email, int size, string defaultImagePath) {
        return GetGravatarURL(email, size) + string.Format("&default={0}", 
                   defaultImagePath);
    }

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

HTHs,
Charles

Charlino
Thanks Charles. This implementation has a potential bug in that the email address is required to be trimmed and lower-cased before being hashed. It also doesn't allow adding additional HTML element attributes (though I didn't mention that in the question!). A good start though, cheers.
Drew Noakes
A: 

Thanks for your answers. In the end I wrote my own solution, so I'll post it here for anyone else who might find it useful.

It caters for all the functionality Gravatar supports right now, as listed in the question.

Use it like this:

<%= Html.Gravatar(Model.User.EmailAddress) %>

I provided optional arguments for any, er, options. These can be combined.

// Use a specific image size (the default is 80px)
Html.Gravatar(Model.User.EmailAddress, size:64)

// Specify what image should appear if the email address is not
// associated with a Gravatar account
Html.Gravatar(Model.User.EmailAddress,
              defaultImage:GravatarDefaultImage.Identicon)

// Specify the maximum rating allowed for images
Html.Gravatar(Model.User.EmailAddress, rating:GravatarRating.Pg)

// Add any additional HTML attributes for the <img /> tag
Html.Gravatar(Model.User.EmailAddress,
              htmlAttributes:new { @class = "gravatar" })

Here's the code:

using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace SampleNamespace
{
    public static class HtmlHelperGravatar
    {
        /// <summary>
        /// Creates HTML for an <c>img</c> element that presents a Gravatar icon.
        /// </summary>
        /// <param name="html">The <see cref="HtmlHelper"/> upon which this extension method is provided.</param>
        /// <param name="email">The email address used to identify the icon.</param>
        /// <param name="size">An optional parameter that specifies the size of the square image in pixels.</param>
        /// <param name="rating">An optional parameter that specifies the safety level of allowed images.</param>
        /// <param name="defaultImage">An optional parameter that controls what image is displayed for email addresses that don't have associated Gravatar icons.</param>
        /// <param name="htmlAttributes">An optional parameter holding additional attributes to be included on the <c>img</c> element.</param>
        /// <returns>An HTML string of the <c>img</c> element that presents a Gravatar icon.</returns>
        public static string Gravatar(this HtmlHelper html,
                                      string email, 
                                      int? size = null,
                                      GravatarRating rating = GravatarRating.Default,
                                      GravatarDefaultImage defaultImage = GravatarDefaultImage.MysteryMan,
                                      object htmlAttributes = null)
        {
            var url = new StringBuilder("http://www.gravatar.com/avatar/", 90);
            url.Append(GetEmailHash(email));

            var isFirst = true;
            Action<string,string> addParam = (p,v) =>
                {
                    url.Append(isFirst ? '?' : '&');
                    isFirst = false;
                    url.Append(p);
                    url.Append('=');
                    url.Append(v);
                };

            if (size != null)
            {
                if (size < 1 || size > 512)
                    throw new ArgumentOutOfRangeException("size", size, "Must be null or between 1 and 512, inclusive.");
                addParam("s", size.Value.ToString());
            }

            if (rating != GravatarRating.Default)
                addParam("r", rating.ToString().ToLower());

            if (defaultImage != GravatarDefaultImage.Default)
            {
                if (defaultImage==GravatarDefaultImage.Http404)
                    addParam("d", "404");
                else if (defaultImage==GravatarDefaultImage.Identicon)
                    addParam("d", "identicon");
                if (defaultImage==GravatarDefaultImage.MonsterId)
                    addParam("d", "monsterid");
                if (defaultImage==GravatarDefaultImage.MysteryMan)
                    addParam("d", "mm");
                if (defaultImage==GravatarDefaultImage.Wavatar)
                    addParam("d", "wavatar");
            }

            var tag = new TagBuilder("img");
            tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            tag.Attributes.Add("src", url.ToString());

            if (size!=null)
            {
                tag.Attributes.Add("width", size.ToString());
                tag.Attributes.Add("height", size.ToString());
            }

            return tag.ToString();
        }

        private static string GetEmailHash(string email)
        {
            if (email == null)
                return new string('0', 32);

            email = email.Trim().ToLower();

            var emailBytes = Encoding.ASCII.GetBytes(email);
            var hashBytes = new MD5CryptoServiceProvider().ComputeHash(emailBytes);

            Debug.Assert(hashBytes.Length == 16);

            var hash = new StringBuilder();
            foreach (var b in hashBytes)
                hash.Append(b.ToString("x2"));
            return hash.ToString();
        }
    }

    public enum GravatarRating
    {
        /// <summary>
        /// The default value as specified by the Gravatar service.  That is, no rating value is specified
        /// with the request.  At the time of authoring, the default level was <see cref="G"/>.
        /// </summary>
        Default,

        /// <summary>
        /// Suitable for display on all websites with any audience type.  This is the default.
        /// </summary>
        G,

        /// <summary>
        /// May contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence.
        /// </summary>
        Pg,

        /// <summary>
        /// May contain such things as harsh profanity, intense violence, nudity, or hard drug use.
        /// </summary>
        R,

        /// <summary>
        /// May contain hardcore sexual imagery or extremely disturbing violence.
        /// </summary>
        X
    }

    public enum GravatarDefaultImage
    {
        /// <summary>
        /// The default value image.  That is, the image returned when no specific default value is included
        /// with the request.  At the time of authoring, this image is the Gravatar icon.
        /// </summary>
        Default,

        /// <summary>
        /// Do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response.
        /// </summary>
        Http404,

        /// <summary>
        /// A simple, cartoon-style silhouetted outline of a person (does not vary by email hash).
        /// </summary>
        MysteryMan,

        /// <summary>
        /// A geometric pattern based on an email hash.
        /// </summary>
        Identicon,

        /// <summary>
        /// A generated 'monster' with different colors, faces, etc.
        /// </summary>
        MonsterId,

        /// <summary>
        /// Generated faces with differing features and backgrounds.
        /// </summary>
        Wavatar
    }
}
Drew Noakes
+1  A: 

Unfortunately, I havent seen your question earlier or I would have recommended you to use my Gravatar.NET library I have built to provide a .NET wrapper for the entire Gravatar API.

If you're interested, check it out here: http://gravatarnet.codeplex.com/

poeticGeek
Thanks for the link. I'll check it out.
Drew Noakes