Generating a gravatar image with php

gravatar

Generating gravatar avatar images using php

An avatar is an image that represents you and your online presence in the web world. Its the little picture that appears to the next of your name when interacting with websites.

A gravatar is a Globally  Represented Avatar . You create a profile and upload it in the gravatar.com website once, and when you participate in any gravatar-enabled websites, your gravatar image will automatically follow you there.

Gravatar is a free service for site owners, developers and users. Gravatar is automatically included in every  wordpress.com account and is run and supported by Automattic.

 

The base

For getting a gravatar image, all you need is the email for which we need to get the gravatar image of.  Gravatar images can be requested just like a normal image, using an IMG tag. but before requesting to get a user image, you should calculate their email hash.

The basic url to the gravatar image will look like this.

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

So lets convert this piece of url into a php code for which we can replace the HASH with a provided email address. The gravatar service uses the php md5() function to calculate the hash for a given string. The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm.

So , in the following code the $url will give out a gravatar image which can be used in an html IMG tag.

<?php
   //provide an email address
   $email="An email address here";

   //code for generating hash
   $hash=md5(strtolower(trim($email)));

   //url for gravatar image
   $url="http%3A%2F%2Fwww.gravatar.com%2Favatar%2F".$hash;
?>

The $url can be used in a IMG src attribute to get a gravatar image for the email address provided.

I have created a function for generating the gravatar in an easier way. The function is as follows.

<?php
function gravatar($email)
{
    $hash=md5(strtolower(trim($email)));
    $url="http%3A%2F%2Fwww.gravatar.com%2Favatar%2F".$hash;
    urldecode($url)
}
?>

The above function can be used in a php page to generate a gravatar image. It is done as follows :

<img src="<?php gravatar([email protected]'); ?>" alt="gravatar"/>

The gravatar service provides many useful ways to customize the image for the needs of the developers. It provides many options like changing the default image, getting a themed image like monsterid, identicon etc. and forcing defaults.

For changing the default image, pass a d= parameter followed by a url encoded image source to make the source image as the default image, when a gravatar image of a user is not available.

To change the theme of a gravatar image default to a custom theme provided by the gravatar pass any of the following keywords to the d= parameter. The available parameters are 404, mm, identicon, monsterid, wavatar, retro, blank .

To force the default image for all user request pass the f= parameter. make it into y or n to force defaults.

I have updated the above function with a small array of available parameters for creating some basic options while generating gravatar.

<?php
function gravatar($email)
{
    $options=array(
        'd' => 'identicon', // default : use a urlencoded source image or a theme name
        'f' => 'y' // force default is set to true
    );
    $hash=md5(strtolower(trim($email)));
    $url="http%3A%2F%2Fwww.gravatar.com%2Favatar%2F".$hash."?".http_build_query($options);
    echo urldecode($url);
}
?>

<img src="<?php gravatar([email protected]'); ?>" alt="gravatar"/>

Hope this helps to generate a gravatar image as the profile picture or display picture for your user management systems in your web applications or personal projects. Gravatar is helpful when we dont have enough storage space to store user profile pictures which puts us in a situation where we will have to disable the user image uploads. So gravatar can be used as the display image for user management systems.

For a sample demo try commenting under this article in the comment section. The picture next to the comment is a gravatar image generated with the email you have provided while commenting.

We will be bringing more tutorials and articles on more services and application programming interfaces(API).  So keep updated by subscribing our newsletter and following our social media channels. Comment for any queries and tutorial requests.

Leave a Reply