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

Concept
:  To obtain your Gravatar, use PHP to create the md5 hash of the email address of interest and append that hash to: gravatar.com/avatars/[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.

Problem:
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:
Medium

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

Implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Enter email address for which you want to get a Gravatar
$email = your@email.com;

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

// define the path for the image
$imLoc = "http://www.gravatar.com/avatar/".$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 '<strong>Please upload your picture</strong>
<label for="file">Filename:</label>

<input id="file" name="file" type="file" /> <input name="submit" type="submit" value="Upload" />'
;

} else {

// Display the Gravatar and ask them if they want to use it
echo '<img class="alignleft" style="margin: 5px;" title="gravatar image" src="http://www.gravatar.com/avatar/'.$hash.'.jpg" alt="gravatar image" width="80" height="80" />
Would you like to use this image as your profile picture?

<input checked="checked" name="UseGravatar" type="checkbox" /> 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)