How to fix Gravatar problem in BlogEngine 2.5

As part of polishing my new blog design I noticed that Gravatar images are not showing up in post comments. Quick glance at themes folder and CommentsView.ascx, no problem there.

Next step, download Blog Engine latest source code from CodePlex to see what happens in code behind.

 

Code for handling Gravtar images is in BlogEngine.Core project in Avatar.cs file.

 Code snippet bellow shows current implementation. 

var gravatar = string.Format("http://www.gravatar.com/avatar/{0}.jpg?s={1}&d=", hash, size);

            string link;
            switch (BlogSettings.Instance.Avatar)
            {
                case "identicon":
                    link = string.Format("{0}identicon", gravatar);
                    break;

                case "wavatar":
                    link = string.Format("{0}wavatar", gravatar);
                    break;

                default:
                    link = string.Format("{0}monsterid", gravatar);
                    break;
            }

            imageTag = string.Format(
                CultureInfo.InvariantCulture, AvatarImage, link, HttpUtility.HtmlEncode(description));

            return new Avatar { Url = new Uri(link), ImageTag = imageTag };

Here is the problem. Gravatar.com no longer supports this kind of URLs.

http://en.gravatar.com/site/implement/images/

The most basic image request URL looks like this:

http://www.gravatar.com/avatar/HASH

where HASH is replaced with the calculated hash for the specific email address you are requesting. For example, here is my base URL:

http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50

When wrapped in an IMG tag, that URL will produce:

<img src="http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50" />

If you require a file-type extension (some places do) then you may also add an (optional) .jpg extension to that URL:

http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50.jpg

Size

By default, images are presented at 80px by 80px if no size parameter is supplied. You may request a specific image size, which will be dynamically delivered from Gravatar by using the s= or size= parameter and passing a single pixel dimension (since the images are square):

http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=200

 

Here is my code snippet which follows current Gravatar API and fix for this problem.

 

var gravatar = string.Format("http://www.gravatar.com/avatar/{0}?s={1}", hash, size);

            string link;
            switch (BlogSettings.Instance.Avatar)
            {
                case "identicon":
                    link = string.Format("{0}identicon", gravatar);
                    break;

                case "wavatar":
                    link = string.Format("{0}wavatar", gravatar);
                    break;

                default:
                    //link = string.Format("{0}monsterid", gravatar);
                    link = gravatar;
                    break;
            }

You can download BlogEngine source code and edit it manually, rebuild and redeploy BlogEngine.Core.dll or you can you download my already compiled version from here: Bin.zip

About me

Bizic Bojan is Co-Founder of Amida IT-Services GmbH and Software Architect with focus on .NET, C++, Python and Cloud Native solutions. 

 

Disclaimer:

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.