PHP – Displaying Gravatars In A Non-WordPress Application

Citing their website, an Automattic.com joint:,

A gravatar, or globally recognized avatar, is quite simply an avatar image that follows you from weblog to weblog appearing beside your name when you comment on gravatar enabled sites. Avatars help identify your posts on web forums, so why not on weblogs?

It’s free and with one account, one can register and assign multiple email address the same (or different) avatar(s).

Why?

For my recent, still on-going, attempts to write a personally satisfying photoblog software/application, I would like to display the gravatar associated with comment author’s email address. Would it attract more comments? May be yes, may be no. Would it make comment authors feel good about it? May be yes. It certainly makes both my blog and photoblog have a nearly consistent theme.

Requirements

It is my practice that I do a full/maximal installation of any linux distribution and that takes care of installing Apache (with all the required modules), PHP, MySQL, ImageMagick, etc. I bet there are tons of documents online that you can refer and install them if you don’t already have them.

DisplayGravatar.php

Let us suppose that all the required code goes into a file called DisplayGravatar.php. One could otherwise put these lines into a global functions page.

1
<!--?php # Function that accepts email address, width &amp; rating as input parameters and returns # the URL to the Gravatar associated with that email address. function display_gravatar ($email, $width, $rating) { # MD5 hashed version of requested email address $hashed_email = md5($email); $gravatar_url = "http://www.gravatar.com/avatar.php?"; $gravatar_url .= "gravatar_id=$hashed_email"; $gravatar_url .= "&amp;rating=$rating"; $gravatar_url .= "&amp;size=$width"; return $gravatar_url; } ?-->

The following is a sample usage of this function:

1
2
3
4
5
6
7
8
<!--?php 
 
  # Get Gravatar URL
  # Change the width and rating, if necessary
  $gravatar_url = display_gravatar($email, '80', 'R');
 
  print "&lt;img src=\"$gravatar_url\" border=\"0\" alt=\"Gravatar\" title=\"Gravatar\" align=\"left\"&gt;";
?-->

Screenshots

When everything goes well, result may look like:


Gravatar

When a gravatar is associated with an email address


Gravatar

When a gravatar is not associated with an email address

2 Replies to “PHP – Displaying Gravatars In A Non-WordPress Application”

  1. @Amy:

    Did you know that your comment (as in the screenshot) was the first ever in my photoblog?? As such, my reply is kinda lame :-P

    The background image for the new theme is taken from some WordPress theme – I like the fact that it tiles well and two column (with sidebar) was to break the monotony of old newspaper like appearance :D

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.