« BASH – GZIP or BZIP2? | WordPress – Legacy of comments.php For 2.7 And Beyond »
PHP – Displaying Gravatars In A Non-WordPress Application
October 20th, 2008 @ 19:01:34 | Linux, PHP, Showcase
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php # Function that accepts email address, width & 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 .= "&rating=$rating"; $gravatar_url .= "&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 "<img src=\"$gravatar_url\" border=\"0\" alt=\"Gravatar\" title=\"Gravatar\" align=\"left\">"; ?> |
Screenshots
When everything goes well, result may look like:
![]()
When a gravatar is associated with an email address
![]()
When a gravatar is not associated with an email address



Shit, you know where to find me now? Ha ha.
Like the new look of the site, BTW.
@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
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
Thank you. Content was very helpfull
These are true mornings of creation, original and poetic days, not mere repetitions of the past.
There is no lingering of yesterday’s fogs, only such a mist as might have adorned the first morning.
Holy crap I love you and want to name children after you. (don’t have any of my own so I’ll just call other peoples’ kids Brian, is that okay?) This is so awesome it’s like bacon and beer together. The most messed up part is this is one thing I would actually use a share button for, because the world needs to know.