Gravatar: Manage your user avatars for free

avatarWhile working on one of the application, I had to manage users who logged in to the application and manage their details. In order to manage user’s avatar, I had to create a form where user can upload a picture and this photo will get stored on server.

Imagine as a user you have to manage this across all the websites where you do login. A good idea is to manage avatar at a single place and all the application that you use can take it from there.

You can achieve this by logging into Gravatar. Gravatar is a free service that let you logged in and manage your avatar. Lot of websites and blogs directly take the avatar from Gravatar. Thus is you comment on a blog like ours and your Gravatar will be displayed directly.

Gravatar does the mapping of user and avatar on basis of email id. Thus when you do login in Gravatar, it stores your avatar on basis of your email address. Hence whenever you provide your email address while you comment on a blog or login to a website, your avatar is taken from gravatar service by mapping your email address.

Gravatar enabled comment systems and other plugins are already available in different CMS systems like wordpress, drupal, joomla etc. All you need to do is to install the plugin and enable it and it will take care of the avatars of users.

You may want to add support to Gravatar on your custom build application/blog. For this all you have to do is to call a URL and it will give you avatar of the user. You have to pass MD5 hash of the email address to this URL. For example following can be the PHP code to generate gravatar from an email address.

Integrate Gravatar with PHP

$email = "[email protected]";
$default = "http://www.somewhere.com/homestar.jpg";
$size = 40;

$grav_url = "http://www.gravatar.com/avatar.php?
gravatar_id=".md5( strtolower($email) ).
"&default=".urlencode($default).
"&size=".$size; 

The above code creates a $grav_url which contains the md5 hash of email address and the size details. This gravatar url can then be used in an <img src=””> tag and the image will be loaded.

Integrate Gravatar with Java

The following class will provide you with a static method that returns the hex format md5 of an input string:

import java.util.*;
import java.io.*;
import java.security.*;
public class MD5Util {
public static String hex(byte[] array) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) { sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3)); } return sb.toString(); } public static String md5Hex (String message) { try { MessageDigest md = MessageDigest.getInstance("MD5"); return hex (md.digest(message.getBytes("CP1252"))); } catch (NoSuchAlgorithmException e) { } catch (UnsupportedEncodingException e) { } return null; } } [/code] This class can then be used to return the MD5 hash of an email address like this: [code="java"] String email = "[email protected]"; String hash = MD5Util.md5Hex(email); [/code] With the hex string that is returned, you can construct your gravatar URL. Thus Gravatar makes it easy for us to manage the avatars at a single place.

Get our Articles via Email. Enter your email address.

You may also like...

1 Comment

  1. arifafzal says:

    wawww its awsome dude i like it too much so i m a new blogger so kindly if u have any good tips for me then plzz tell me

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.