Easily Add Gravatar to Your PHP Apps
Among the few hassles to being a citizen of the Internets, along with multiple login credentials, comment trolls and pedophiles (obviously), is the hassle of multiple profile pics. Yes, it’s an extremely small problem but it is a problem solved by Gravatar.
Gravatar, which stands for Globally Recognized Avatar (seriously), is a service provided by Automattic (yes, the same Automattic that provides WordPress) that allows users to upload a single image which developers can then use on their websites. The outcome is that users have a single icon (or avatar) representing them, that they don’t have to repeatedly upload. Another, less publicized benefit, is that us developers don’t have to write all the photo uploading and resizing functionality. Again.
From an implementation standpoint Gravatars couldn’t be simpler. At the base level it’s just a call to a URL that includes an md5() hash of a users email. For example my gravatar URL is the below:
Looking at the URL (which hopefully, you’re comfortable looking at) it’s broken up like so:
Base URL: http://www.gravatar.com/avatar/
My Identifier (email@example.com in md5()): 8a55f4e419d6d5314e420ba26bf7455e
Query String Parameters: s=75&d=wavatar&r=X
As far as the query string goes, none of the below are required but using them allows to customize the output. You have the following options:
Image Size: s (between 1 and 512)
Rating: r (think movie ratings with G – X)
Considering all of the above, and how simple it is, I’m kind of amazed that there’s still an issue with wide adoption (though this could be due to Gravatar scaling the service for the obscene amount of traffic this would create) .
Obviously, writing a little snippet to create the Gravatar URLs in PHP would be trivial to most programmers:
<?php $url = 'http://www.gravatar.com/avatar/'; $email = 'firstname.lastname@example.org'; $default = 'monsterid'; $size = 75; $grav_url = $url.'?gravatar_id='.md5( strtolower($email) ). '&default='.urlencode($default).'&size='.$size; ?> <img src="<?php echo $grav_url;?>" />
Simple but the code above could be a lot more useful. As expected though, a few php classes have been written that’ll handle all the particulars for you. One, available on phpclasses.org, I looked at, and even used within a project, but it didn’t have native support for “defaults” so I decided against using it as an example here (still a cool script if the next one doesn’t float your boat).
The other script is available through TalkPHP, so it’s not really clear who the actual author is (sorry for not giving credit whoever you are). The code is clean, clear and dead easy to implement though.
<?php include_once('./TalkPHP_Gravatar.php'); $pAvatar = new TalkPHP_Gravatar(); $pAvatar->setEmail('email@example.com')->setSize(80)->setRatingAsPG()->setDefaultImageAsIdentIcon(); ?> <img src="<?php echo $pAvatar->getAvatar(); ?>" />
Very, very simple. I have to say I’m a fan of Gravatar. I wish more sites implemented Gravatars, instead of having me constantly uploading a different image, but now you can do your part. Get cracking.