sgowtham.net

Brain Dump v1.0  :: Rambling About Life’s Little Things

« Previous Post | Next Post »


PHP - Displaying Gravatars In A Non-WordPress Application

2008.10.20 @ 19:01:34 -0500 under  Linux, PHP, Showcase | 2 Comments

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:



Gravatar

When a gravatar is associated with an email address



Gravatar

When a gravatar is not associated with an email address

divider


Opinions expressed in these pages are purely personal and do not reflect those of any other institution and/or individual
© 2002-2018 Gowtham • All Rights Reserved