- a developer's blog

Display a User Gravatar from an Email Address | PHP Class

Gravatars add a personal touch to any discussion. You will invariably notice a greater level of reader involvement on blogs that show gravatar image. Fortunately pulling gravatar image with PHP is quite simple.

Here’s the class

class addGravatar {

public function validate_email($useremail, $test_mx = false) { 
/* checks if an email address is in valid format
 and if it has a valid mxrecord*/
(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $useremail)
		list($username, $domain) = split("@", $useremail);
		return getmxrr($domain, $mxrecords);
		return true;
		return false;

public function display_gravatar($useremail, $size, $default, $rating) { 
/* display user gravatars */
echo '';

If the email id does not have an associated Gravatar account, it returns a default image.