How to Detect a Default Gravatar Image for an Email Address – PHP Tutorial

Summary:  Have you ever liked a site so much that you created an account on their website by giving them your email and creating a password, only to login and find that they already had a picture of you on file?  How do that do that?!   This intelligent design utilizes a free web service called Gravatar (or similar concept).

:  To obtain your Gravatar, use PHP to create the md5 hash of the email address of interest and append that hash to:[insert-hash-here].jpg.  There are a lot of available options for the type of image that is called (by passing GET parameters) from the Gravatar servers that can adjust the size, type, etc of the image.

I went to implement this system and it worked GREAT, however what I couldn’t find on Gravatar’s site or any other is how to detect if an image is the default image.  In my case, if the email addressed returned the default Gravatar’s logo then I didn’t want the image to show up; if it DID return a unique image, I wanted to display that image and ask the user if they wanted to use it as their profile pic.

Difficulty Level:

Prerequisites: To do this you must have PHP’s GD Image library installed on your server.


[cc lang=”php” escaped=”true”]

// Enter email address for which you want to get a Gravatar
$email =;

// md5 hash the email address
$hash = md5( strtolower( trim($email) ) );

// define the path for the image
$imLoc = “”.$hash.”?s=150?d=404″;

// Create an image from the path’s source
$im = imagecreatefromjpeg($imLoc);

// Check the pixel color at the very top-left corner of the image
$rgb = imagecolorat($im, 1, 1);

// Get the Red value of the pixel
$r = ($rgb >> 16) & 0xFF;

// Get the Green value of the pixel
$g = ($rgb >> 8) & 0xFF;

// Get the Blue value of the pixel
$b = $rgb & 0xFF;

// If this is a default Gravatar image (RGB=10,118,183) then display
// the default image and ask them to upload an image
if($r==10 && $g==118 && $b==183) {

// Ask them to upload an image
echo ‘Please upload your picture


} else {

// Display the Gravatar and ask them if they want to use it
echo ‘gravatar image
Would you like to use this image as your profile picture?

Yes, use this as my profile picture.’;


Explanation: Essentially, this code downloads the Gravatar image for the email specified, checks the pixel value for Default Gravatar Imagethe very top-left corner of the image, and then if the RGB values match EXACTLY to what the default image is, we know (or at least we are pretty darn sure!) that the image returned is the default Gravatar logo.  

Summary: I could not find any other solutions for solving this problem anywhere on the web, so I hope this helps somebody out there who has a similar problem, and if you know of any other solutions please leave the URL in the comments section of this post.  What do you guys think?

About Andrew Turner (aka Arcitech)

2 Responses to “How to Detect a Default Gravatar Image for an Email Address – PHP Tutorial”

Read below or add a comment...


  1. […] How to Detect a Default Gravatar Image for an Email Address – PHP … […]

Leave A Comment...