Home > Tutorials > PHP Tutorials > Get A Users Gravatar Image Via PHP

Get A Users Gravatar Image Via PHP

Gravatar’s have been popular for years now – and to date they’re integrated across a plethora of websites – serving over 8.6 million avatars every single day. With that said there are also a ton of plugins available for most blog and CMS scripts; however what if no plugin exists for our website, or we simply want to do it ourselves? Luckily Gravatar has an extremely easy to use API, and in this tutorial we will go over how to use PHP to display a users Gravatar image.

First off we must understand that Gravatars work off a users registered email address – so in order for us to go further we have to make sure that we are, somehow, getting the users email address. Gravatar then creates an MD5 hash of a users email address and stores their Gravatar image under that name. Luckily PHP has a built-in MD5 function which we can make use of to convert our email address. BUT FIRST we also have to convert our email address to all lowercase. See MD5 will generate a different hash for uppercase letters than lowercase letters, and to match Gravatar we need all lowercase.

$userEmail = "[email protected]";
//convert email to lowercase
$userEmail = strtolower($userEmail);
//convert to MD5 Hash
$userEmailHash = md5($userEmail);

Now we have an MD5 hash (like 14c4b06b824ec593239362517f538b29). If we go find a Gravatar in-use anywhere else and view the URL for the image we will see the following:

http://www.gravatar.com/avatar/14c4b06b824ec593239362517f538b29

So all we need to do is take our MD5 hash and create the same URL with it, and embed it onto our website with an IMG tag:

echo "<img src='//gravatar.com/avatar/$userEmailHash'>";

Technically we can end right there – but there’s a few more settings that the Gravatar API allows you to make use of – and If you’re interested feel free to read on.

Setting The Size

The Gravatar API will, by default, show an image that is 80×80 – however if you want to show something smaller, or larger, you can do so using the API. For this we’re going to add to our Gravatar URL the parameter s and give it our desired size:

//set a size variable
$size = "150";
//echo gravatar
echo "<img src='//gravatar.com/avatar/$userEmailHash?s=$size'>";

Setting A Default Avatar To Display

Granted Gravatar’s are extremely popular – but not everyone is going to have one. With that in-mind we need to fail gracefully if we run this function on an email address that doesn’t have a Gravatar. For this we can use the d parameter and give it the URL of your default avatar. Since we’re going to be passing a full URL we’ll want to URL-encode our default avatar link to ensure the script functions properly.

$defaultAvatar = "http://icons.iconarchive.com/icons/guillendesign/variations-3/256/Default-Icon-icon.png";
//convert default avatar URL to urlencoded
$defaultAvatar = urlencode($defaultAvatar); 
//echo gravatar
echo "<img src='//gravatar.com/avatar/$userEmailHash?d=$defaultAvatar&s=$size'>";

In the end here is the full code breakdown. If you want you can also fork the code over at Github.

//start with some settings
$userEmail = "[email protected]";
$size = "150";
$defaultAvatar = "http://icons.iconarchive.com/icons/guillendesign/variations-3/256/Default-Icon-icon.png";


//convert email to lowercase
$userEmail = strtolower($userEmail);
//convert to MD5 Hash
$userEmailHash = md5($userEmail);

//convert default avatar URL to urlencoded
$defaultAvatar = urlencode($defaultAvatar); 

//echo gravatar
echo "<img src='//gravatar.com/avatar/$userEmailHash?d=$defaultAvatar&s=$size'>";

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

5 Must Have WordPress Optimization Plugins For A Fast Loading Website

Having a fast loading website is essential these days. First off, users are impatient. If ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close