Announcing Get Gravatar
![]()
Tim was working on something cool the other day that gave me an idea for a plugin to fetch a Gravatar via AJAX. After a couple hours of work, i’m proud to present Get Gravatar, a jQuery plugin for Gravatars.
Gravatar is a service for universal avatars. If you don't have one, go get one here.
Get Gravatar is meant to be used with a text input. As the text changes in the input, it pings the Gravatar service to fetch the user’s avatar. Due to Gravatar’s restrictions, Gravatars must be retrieved with an MD5 hash of the user’s email. Since Javascript doesn’t have a built-in way to fetch an MD5 hash, this plugin has a PHP counterpart.
Parameters
- url (string): the URL to the PHP counterpart. You can use either a relative or exact URL.
- fallback (string): URL to your placeholder image for if the user does not have a Gravatar. Must be an exact URL.
- avatarSize (number, 0-512): Size in pixels of the gravatar to fetch.
- avatarContainer (string): jQuery style selector of the
tag to put the gravatar in once it’s loaded. Default is "#gravatar".
- start (function): Function to run when it starts loading. Perfect place to put code for a Loading indicator.
- stop (function): Function to run when it’s done loading. Perfect place to put code to hide a Loading indicator.
Code examples:
Barebones usage:
$("#email-address").getGravatar();
Simple parameters:
$("#email-address").getGravatar({
url: '/includes/get-gravatar.php',
fallback: 'http://myurl.com/images/default.png'
});
More parameters:
$("#email-address").getGravatar({
url: '/includes/get-gravatar.php',
fallback: 'http://myurl.com/images/default.png',
avatarSize: 80,
avatarContainer: '#gravatar-container'
});
Specify start and stop functions
$("#email-address").getGravatar({
start: function(){
$("loading").show();
},
stop: function(){
$("loading").hide();
}
});
Other non-sense:
This plugin is pretty versatile. You can access the basic functions of the plugin if you really want to. You can also use this plugin with the jQuery metadata plugin.
Tim Van Damme has prepared a fun demo for your viewing pleasure.
You can download the plugin below:
10/28/09: Updated to 1.2
ZIP Format, 41 kb
Or check it out on Github.



5 comments
Michael Sigler
Jul 21, 2009Nicely done Josh. I’m surprised I haven’t seen this one out there before. Nice thinking!
Adam Wride
Jul 23, 2009Would be pretty rad if the same plugin (or extended plugin) supported Twitter avatars. If you do @adamwride then you see my twitter avatar. =)
(JavaScript must be enabled to view this email address)
Jul 23, 2009Mike: Thanks man. I was surprised as well!
Adam: That’s a pretty good idea as well. Although it would be a completely different process, it would be just about as easy using the Twitter API. The only issue would be sizing the avatars. I believe without authorization you can only grab 40x40 images, but that’s plenty big enough for most uses. Might put that down as something to play with in the future.
Christopher Hill
Aug 3, 2009Since MD5 is simply an algorithm you can implement it in Javascript. E.g. http://phpjs.org/functions/md5:469
(JavaScript must be enabled to view this email address)
Aug 3, 2009Hey Chris,
Interesting. I wonder if it’s ok to copy and paste this function into my plugin. I’ll have to contact someone and find out.