Web Development

Get Gravatar Image with PHP

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.

[cc lang=”php”]

$email= ” [email protected] “;

// Removing Spaces
$email = trim($email);

// Make all Lower Case
$email = strtolower($email);

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

[/cc]

Making Simple Request

Please find below structure of the simple request for Gravatar.

[code]http://www.gravatar.com/avatar/EMAIL_HASH[/code]

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.

[cc lang=”html”]



PHP GravatarPHP GravatarPHP GravatarPHP Gravatar

Shares:
  • Ashish
    January 25, 2012 at 12:00 am

    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….

    Reply
  • Ashish
    December 20, 2019 at 2:54 pm

    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….

    Reply
  • EdwinChui
    EdwinChui
    January 27, 2012 at 10:20 pm

    Thx for your sharing. I try now!

    Reply
  • EdwinChui
    EdwinChui
    December 20, 2019 at 2:54 pm

    Thx for your sharing. I try now!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *