Get Gravatar Image with PHP

  • Avinash
  • 2
  • Nov 16, 2011
  • Web Development

Gravatar stands for Globally Recognized Avatar. This is the very well known service to show your avatar globally.

You can get Gravatar using an email address. In this post I am going to show you how to show the gravatar using an email address.

I am going to cover this post with PHP.

Get Prepared

To get the Gravatar your must need an email address of which you want to get avatar. After getting an email address we need to create a hash for that email address.

But before creating hash you need to perform some filtering operation on email address. These filtering are like removing all leading and trailing spaces from email address and make sure that all characters are in lower case.

Now for generating hash, means MD5 hash of the filtered email address. Let’s see the below code snippet for the same.

$email= " [email protected] "; // Removing Spaces $email = trim($email); // Make all Lower Case $email = strtolower($email); // Generating Hash $email_hash = md5($email);
  $email= " [email protected] ";
 
  // Removing Spaces
  $email = trim($email);
 
  // Make all Lower Case
  $email = strtolower($email);

  // Generating Hash
  $email_hash = md5($email);

Making Simple Request

Please find below structure of the simple request for Gravatar.

http://www.gravatar.com/avatar/EMAIL_HASH

Here EMAIL_HASH will be replaced by the hash of the filtered email address. We can place this url in src tag of the img tag like below.

<img src="http://www.gravatar.com/avatar/<?php echo $email_hash?>" />

Defining the Size

We can define the size of the avatar which we want to receive. Default would be 80px*80px if we do not pass the size parameter.

We have to pass the parameter s in the image source like below.

<img src="http://www.gravatar.com/avatar/<?php echo $email_hash?>?s=100" />

We can define size from 1px to 512px.

Define Default Image

We can define default image using default or d parameter. You can pass the path to the default image in this parameter. Make sure that you encode the path of the default image.

It will serve this default image if avatar with the given email address is not found.

<?php
 $path = "http://www.gravatar.com/avatar/".$email_hash;
$d = urlencode("http://domain.com/default.jpg");
?>
<img src="<?php echo $path?>/?d=<?php echo $d?>" />

Even we can pass the default image, Gravatar has own set of default images. They have given them a various name like below.

1) 404
2) mm
3) identicon
4) monsterid
5) wavatar
6) retro

We can pass above category in default parameter to load the random image based on category.

<?php
$path = "http://www.gravatar.com/avatar/".$email_hash;
?>
<img src="<?php echo $path ?>/?d=404" />
<img src="<?php echo $path ?>/?d=mm" />
<img src="<?php echo $path ?>/?d=identicon" />
<img src="<?php echo $path ?>/?d=monsterid" />
<img src="<?php echo $path ?>/?d=wavatar" />
<img src="<?php echo $path ?>/?d=retro" />

Force Default Image

If we pass this parameter then it will serve the default image even if it has found the avatar associated with the given email address. This parameter is f or forcedefault.

<?php
$path = "http://www.gravatar.com/avatar/".$email_hash;
?>
  <img src="<?php echo $path?>/?d=404&f=y" />

Get you Gravatar from here, if you don’t have yet.

Demo

PHP GravatarPHP GravatarPHP GravatarPHP Gravatar

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • http://10pixel.com Ashish

    Wow, this way, now it’s easy to use gravtar in .net as well.
    With jQuery we can get the images, and can be integrated easily.
    thanks….

  • EdwinChui

    Thx for your sharing. I try now!