« BASH - GZIP or BZIP2? | WordPress - Legacy of comments.php For 2.7 And Beyond »
PHP - Displaying Gravatars In A Non-WordPress Application
2008.10.20
@ 19:01:34 -0400 under
Linux, PHP, Showcase
Disclaimer
The instructions/steps/scripts/methods given below worked for me running CentOS. It may very well work for you on other linux distributions, Red Hat-like or otherwise. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.net, nor its author is responsible for any/all damage - intellectual or otherwise.
What is Gravatar?
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
hi there!
xxoxo
I made on photoshop glitter myspace pics.
have a look at them:
http://tinyurl.com/5pq6aj
Thank you for your website
Cheers!
xxoxo
I made on photoshop animated myspace pictures.
have a look at them:
http://tinyurl.com/65lf75
Thanks a lot 4 your site
Thank you. Content was very helpfull