How To Suppress The Gravatar Default Icon

August 4 2011

Gravatar is a nifty service. But if someone on your site doesn’t have a Gravatar, Gravatar will display a default icon.  You have some choices in what gets displayed (documented here), but what if you want nothing displayed?  Well, you can have Gravatar return you a 404, but you need to handle that 404 somehow. Not a big deal, but here’s how I did it.

First off, I started by using this code, which is an ASP.NET control that encapsulates all the Gravatar logic. Then, I mucked around with the override of Render, adding the following:

if (!string.IsNullOrEmpty(DefaultImage))
{
    imageUrl += "&default=404"; // +HttpUtility.UrlEncode(DefaultImage);
}
using (WebClient client = new WebClient())
{
    try
    {
        client.DownloadData(imageUrl);

    }
    catch (WebException webException)
    {
        noImage = true;
    }

}



if (noImage)
{
return;

}

Yes, it means I have to make a call to Gravatar in order to determine to not provide an image, but it is worth it. If performance is an issue, an optimization would be to cache the emails that have already been checked. In fact, I’ll probably add that.  No more default gravatars!

8/16/2011 5:16:22 PM #

Московские путаны

Gravatar is a nifty service.  But if someone on your site doesn’t have a Gravatar, Gravatar will display a default icon.   You have some choices in what gets displayed (documented here), but what if you want nothing displayed?  Well, you can have Gravatar return you a 404, but you need to handle that 404 somehow.

Московские путаны

8/31/2011 1:40:26 PM #

tobi

This is incredibly slow. I see people abusing this code and running it 20 times per request.

tobi

Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.



biuquote
Loading