Integrating Gravatar with PHP

Gravatar is an image that follows you from site to site appearing beside your name when you do things like comment or post on a blog. Gravatar was started by Tom Preston-Werner but it is now owned by “Automattic” which is the company behind WordPress. Today, there are a many of people using this service. Because using it you can use the same avatar for different websites without even uploading an image to the website.

In simple word, Gravatar is Awesome and integrating Gravatar with PHP is easy too.

Gravatar

Gravatar

We are gonna making a simple PHP application that load user avatar associated with email. It is pretty simple process to use Gravatar with PHP. So let’s get start.

function get_avatar($email){
	$size = 164;
	$default = "/blog/images/logo.png";
	$avatar = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( $email ) ) ) . "?d=" . urlencode( $default ) . "&s=" . $size;
	return $avatar;
}

We can fetch avatar from Gravatar using above simple function which will call from element. It convert each email into MD5 hash format so we need to use this hash to load image.


Finally we are going to display email associated avatar.

Avatar Couldn't Loaded.

Leave a Reply

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