The Six Phases of a Project: Enthusiasm. Disillusionment. Panic. Search for the Guilty. Punishment of the Innocent. Praise for non-participants

The Cambia.Gravatar Nuget Package

Copyright © 2016 Steve Lautenschlager

Gravatars are globally recognized avatars in common use on websites around the globe. This simple nuget package provides a .NET class for easily generating Gravatar URLs.

Introduction

Gravatars allow you to display an avatar image associated with a user simply by hashing an email address.

Users have complete control of avatar images at http://www.gravatar.com. Wherever gravatars are used, a user's image will update to use the new one on all websites where gravatars are used.

Here are some samples of my gravatar:

The associated HTML look like this:


  <img src='https://www.gravatar.com/avatar/49ef413fa96d874b1ec80b529c16c7a4?s=20&r=pg&d=mm' />
  <img src='https://www.gravatar.com/avatar/49ef413fa96d874b1ec80b529c16c7a4?s=80&r=pg&d=mm' />
  <img src='https://www.gravatar.com/avatar/49ef413fa96d874b1ec80b529c16c7a4?s=150&r=pg&d=mm' />
  <img src='https://www.gravatar.com/avatar/49ef413fa96d874b1ec80b529c16c7a4?s=300&r=pg&d=mm' />


Get the Cambia.Gravatar Nuget Package (Free)

You can download the Cambia.Gravatar Nuget Package from Nuget.org.

Generate Gravatar URL (Basic)

The easiest way to create the gravatar URL using the Cambia.Gravatar nuget package is as follows:


using Cambia;     

Gravatar g = new Gravatar("joe@email.com");
string url = g.GetUrl();

// url = https://www.gravatar.com/avatar/ea6ecebb47cc1b53dd6cf9a523c10b85?s=80&r=pg


This is what it displays:

Wait a minute, that doesn't look like a guy named Joe. No, Joe apparently hasn't setup a gravatar image associated with the fake email address above.

Therefore Gravatar delivers the rotated G icon as a default image.

I'll show you how to change the default image below.

Generate Gravatar URL (All Options)

While an email address is all that is required to get a Gravatar URL you make want more control over a few things. Here's the list of options you can tweak:

  • Email
  • Size (in pixels)
  • Maximum Allowed Image Rating (g, pg, r, x)
  • Default Option (type of image to show when none is available)
  • Default URL (the URL of your custom default image)
  • Force the Default (Show the default image even if a valid image is available)

This webpage on the Gravatar website specifies the exact format for Gravatar URLs and the available options: http://en.gravatar.com/site/implement/images


using Cambia;     

string defaultImage = "https://www.cambiaresearch.com/sites/cambia/r/logo/cr_icon2.png";
Gravatar g = new Gravatar("joe@email.com", 256, GravatarRating.R, GravatarDefaultOption.Custom, defaultImage, false);
string url = g.GetUrl();

// url = https://www.gravatar.com/avatar/ea6ecebb47cc1b53dd6cf9a523c10b85?s=256&r=r&d=http:%2f%2fwww.cambiaresearch.com%2fsites%2fcambia%2fr%2flogo%2fcr_icon2.png


Here's what we get with a default image specified:

In the above example we specified a custom URL for when a valid image doesn't exist. Since no valid gravatar image exists, the default Cambia Research icon is displayed which is the URL that we specified.

There are other default options, too, some of which will generate a unique image for an email address so that you don't just have the same boring default image for people who haven't setup gravatar images yet.


g = new Gravatar("joe@email.com", 60, GravatarRating.R, GravatarDefaultOption.Identicon);
g = new Gravatar("joe@email.com", 60, GravatarRating.R, GravatarDefaultOption.Blank);
g = new Gravatar("joe@email.com", 60, GravatarRating.R, GravatarDefaultOption.MonsterId);
g = new Gravatar("joe@email.com", 60, GravatarRating.R, GravatarDefaultOption.MysteryMan);
g = new Gravatar("joe@email.com", 60, GravatarRating.R, GravatarDefaultOption.Option404);
g = new Gravatar("joe@email.com", 60, GravatarRating.R, GravatarDefaultOption.Retro);
g = new Gravatar("joe@email.com", 60, GravatarRating.R, GravatarDefaultOption.Wavatar);
g = new Gravatar("joe@email.com", 60, GravatarRating.R, GravatarDefaultOption.None);


These default options yield the following:

With a different email address you might get:

The fifth item appears to be a broken image. That was the 404 option. In this case the Gravatar site returns HTTP 404 instead of an image.

Static Methods

There are several static methods on the Gravatar class to support generating the gravatar URL or parsing the query parameter values.


// Generate the email hash
string hash = Gravatar.HashEmailForGravatar("joe@email.com");

// Generate the full URL.  Allows you to generate a URL without instantiating a class if you prefer.
string url = Gravatar.GenerateUrl(...)

// Gets the query value for the "d", default, query item.  The following code would return
// blank because that's what you'd insert into the URL
// e.g. query = ?s=80&r=pg&d=blank
string queryValue = Gravatar.GetDefaultOptionValueForUrl(GravatarDefaultOption.Blank);

// Parses the default option value from the url into the associated enumeration.  
// GravatarDefaultOption.None if the value does not match a known default type
GravatarDefaultOption opt = Gravatar.ParseDefaultOptionType("wavatar");

// Parses the rating value from the url into the associated enumeration.  
// GravatarRating.G if the value does not match a known rating.
GravatarRating rating = Gravatar.ParseRatingValue("pg");

// Parses the value extracted from the gravatar URL for the size key, "s".
// Returns the default size of 80 if the value can't be parsed.
int size = ParseSizeValue("256");

// Parses the force default query value, "f".
// Returns true if the value is "y" or "Y", otherwise false
bool forceDefault = Gravatar.ParseForceDefaultValue("true");