techvigil-logo

Gravatar is a service for providing globally unique avatars associated with emails. Users register their account and attach their desired thumb image for particular email address. A third party program can access their thumb image according to their e-mail. This feature is mostly used in blogs for getting avatar in comments.

The Gravatar image of any person can be easily get if his/her e-mail ID is known. The image request URL for the Gravatar is a function of the e-mail address. Let's see how it works.

Requesting Gravatar Image

Consider an example e-mail address aBc@xyz.com for which we want to get Gravatar image.

First make sure that e-mail address in trimmed. Trimmed means no leading or trailing white-spaces. In this case it is already trimmed. The e-mail address should all in lower-case. So, convert aBc@xyz.com to abc@xyz.com

Now get the MD5 hash of the trimmed and lower-cas e-mail address. Here in this case we have to find MD5 hash of the abc@xyz.com address. Getting MD5 is not a big deal. This function is available in all programming languages. You can get what is for your purpose. In PHP and some other languages you can simply do this:

md5("abc@xyz.com")

This MD5 hash will return you a 32-digit hexadecimal number called as HASH. Suppose we get this as HASH:

4e8fb772f3a4034906153f2d4258ee5c

Now you can request the Gravatar server for image. For this example the image request URL will be:

http://www.gravatar.com/avatar/4e8fb772f3a4034906153f2d4258ee5c

This URL will produce the image for above e-mail. You can use the same as a source of IMG tag of HTML as this:

<img src="http://www.gravatar.com/avatar/4e8fb772f3a4034906153f2d4258ee5c" />

Customizing Gravatar Request

If you require a particular file-type extension for image then you may also add an image extension to the URL like this:

http://www.gravatar.com/avatar/4e8fb772f3a4034906153f2d4258ee5c.png

By default all images are served at 80px by 80px. However Gravatar supports size from 1px to 512px. Size specific image can be requested by adding 's' or 'size' parameter to the URL.

http://www.gravatar.com/avatar/4e8fb772f3a4034906153f2d4258ee5c.png?s=200

However if an email address is not registered with Gravatar or image is not set, this default image will be produced.

For SSL Pages

If you are displaying this on a SSL page, then for better security you'll definitely wish to serve your Gravatars via SSL as well. If so, simply use this URL structure that starts with https://secure.gravatar.com/ like this:

https://secure.gravatar.com/avatar/4e8fb772f3a4034906153f2d4258ee5c
Post tagged in: CodesInternet