Bright Hub
 
Matthew Casperson's Hubfolio

Displaying Gravatar images with PHP

Article by Matthew Casperson (4,883 pts )
Published on Nov 2, 2009

This free tutorial shows you how to display Gravatar images with PHP.

Introduction

VIEW THE DEMO

DOWNLOAD THE SOURCE CODE

A big issue with web sites today is that they repeatedly ask you for the same information over and over again. You fill in your name, your email, upload a photo, invite some friends, blah, blah, blah. I must have run through this process 20 times.

Gravatar aims to solve at least a small part of this problem by providing a central location to manage an avatar image. You upload an image and register it against an email address, allowing web sites to display your avatar against any content that can be attributed to your email address. This can be used to provide an image against blog comments, or in place of yet another custom avatar in a site profile.

For web developers, displaying the Gravatar image is quite simple. Here we will use PHP to map an email address to a face.

Gravitar Screenshot

Step 1 - Get the email address

The first thing you need is the email address, which will be supplied by the user. Hre we grab an email address from the URL email parameter, but you will probably get this from a database in your own application.

$email = $_GET["email"];

Step 2 - Calculate the MD5 hash

Next you need to convert the email address to an MD5 hash. PHP has the md5 function which makes this calculation easy.

$emailMD5 = md5($email);

Step 3 - Build the image URL

Finally, the MD5 hash is used to create a URL where the image resides.

$gravitarURL = "http://www.gravatar.com/avatar/" . $emailMD5 . ".jpg";

If the user has not set up a Gravatar account, you can fall back onto a default image of your choice using the d parameter.

$gravitarURL = "http://www.gravatar.com/avatar/" . $emailMD5 . ".jpg?d=http%3A%2F%2Fwebdemos.sourceforge.net%2FPHP%2FGravitar%2Fgravatar-blank.jpg";

This URL is then used as the src attribute for an img element.

<img src="<?php print htmlspecialchars($gravitarURL); ?>"/>

Conclusion

Gravatar offers a number of additional options, which you can find here. You can return images of different sizes, as well as returning images with different classifications like g, pg, r, or x (matching movie classifications).

Gravatar frees web developers from having to manage avatar images. It also means that users aren’t forced to upload their avatar images over and over again. It's a convenient way to centralise avatar management.

Related Article

flexCreating a Digg client with Adobe Flex in 10 steps

This tutorial steps you through the process of creating a Digg client with Adobe Flex.

Search More About:

 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2009 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape