Code a Dynamic Photo Login Box using Gravatar

The community of global avatars through Gravatar actually started out very small. But the popularity of such an application has grown to scale millions of users with many more millions of different avatars. You can perform some really cool effects with gravatar photos, and I want to demonstrate a small sample in this tutorial.

We will be coding a simple user login box with e-mail/password fields. After the user enters a successful e-mail address we’ll pull any Gravatar associated with the account and display the image results via Ajax. In order to pull the MD5 hash we’ll use a couple lines in PHP. However even a complete beginner shouldn’t get lost since the source code is very structured and easy to understand.

Live DemoDownload Source Code

Starting the HTML

In this demo I’m using a very simple centered container area housing the login form. In the header I’ve linked an external stylesheet file styles.css, along with including the latest jQuery library through Google’s CDN.

The form itself is broken up into horizontal sections with a div at the top for the user avatar image. By default we’ll display the generic Gravatar icon and only try to update after the user has typed a valid e-mail address.

<div id="container">
	<form id="login" name="login" autocomplete="off" method="POST" action="index.html">
		<div id="avatar-box">
			<img src="" alt="Gravatar" id="userimg">
		<div class="input-field">
			<label for="email">e-mail</label>
			<input type="email" name="email" id="email" class="basic">
		<div class="input-field">
			<label for="password">password</label>
			<input type="password" name="password" id="password" class="basic">
		<input type="submit" id="formsub" value="Login!">

I’ll target the e-mail input field with the ID attribute and we can check the value using jQuery. But first let me point out some notable expenses in the stylesheet.

Layout Formatting

In the stylesheet document I’ve attached a repeating background image tile similar to Apple’s iCloud website. I’ve tried to match the input fields with a similar color and dimension of design aesthetics.

form input.basic { 
width: 320px; 
outline: none;
margin-bottom: 10px; 
border: 1px solid #d2dadd;
-moz-box-shadow: 0px 1px 1px #f3f4f6 inset;
-webkit-box-shadow: 0px 1px 1px #f3f4f6 inset;
box-shadow: 0px 1px 1px #f3f4f6 inset;
font-size: 13px;
color: #3a3a3a;
padding: 8px 7px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px 3px 3px 3px;
outline-color: #e2eaed;
background: #f8f8f8; 

Underneath both the form fields I’m also using a set of CSS gradients to design the button. Hover and active events trigger to reverse the glossy gradient and appear more flat. Also the slight text shadow is a clever touch, and admittedly this is one of the first page elements you’ll notice.

#formsub { 
position: relative;
left: 330px;
padding: 9px 22px;
font-size: 1.4em;
cursor: pointer;
border: 1px solid #dedede;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px 3px 3px 3px;
color: #3d5086;
background: #829bf7;
background: -moz-linear-gradient(top, #829bf7 0%, #5f84da 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#829bf7), color-stop(100%,#5f84da));
background: -webkit-linear-gradient(top, #829bf7 0%,#5f84da 100%);
background: -o-linear-gradient(top, #829bf7 0%,#5f84da 100%);
background: -ms-linear-gradient(top, #829bf7 0%,#5f84da 100%);
background: linear-gradient(top, #829bf7 0%,#5f84da 100%);
border-color: #6f8cdf #5f82da #5066d5;
text-shadow: 0 1px 0 #9caff8;
-webkit-box-shadow: 0 1px 1px #c1c1c1, inset 0 1px 0 #a1baf9;
-moz-box-shadow: 0 1px 1px #c1c1c1, inset 0 1px 0 #a1baf9;
box-shadow: 0 1px 1px #c1c1c1, inset 0 1px 0 #a1baf9;
#formsub:hover {
background: #5f84da;
background: -moz-linear-gradient(top, #5f84da 0%, #829bf7 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#5f84da), color-stop(100%,#829bf7));
background: -webkit-linear-gradient(top, #5f84da 0%,#829bf7 100%);
background: -o-linear-gradient(top, #5f84da 0%,#829bf7 100%);
background: -ms-linear-gradient(top, #5f84da 0%,#829bf7 100%);
background: linear-gradient(top, #5f84da 0%,#829bf7 100%);
border-color: #6083c3 #567ac2 #4e71c1;
text-shadow: 0 1px 0 #9caff8;
color: #2e4073;
-webkit-box-shadow: 0 1px 1px #c3c3c3, inset 0 1px 0 #a1baf9;
-moz-box-shadow: 0 1px 1px #c3c3c3, inset 0 1px 0 #a1baf9;
box-shadow: 0 1px 1px #c3c3c3, inset 0 1px 0 #a1baf9; 
#formsub:active {
background: #5f84da;
-webkit-box-shadow: 0 1px 1px #fff, inset 0 1px 0 #a1baf9;
-moz-box-shadow: 0 1px 1px #fff, inset 0 1px 0 #a1baf9;
box-shadow: 0 1px 1px #fff, inset 0 1px 0 #a1baf9;  

Many of these attributes can be updated or replaced to suit your own website. Color schemes and design styles will change, but you can always mix-and-match to suit your desires. Now that we have the basic HTML5/CSS3 layout developed it’ll be easier explaining how we can connect into Gravatar with jQuery and PHP.

Update on Keystrokes

Each time the user presses a key when typing inside the e-mail field we want to check their current value against a generic e-mail address. There are many regex commands to use, and for this tutorial we’ll look at a super simple example.

		var addr  = $(this).val();
		var regex = /(.+)@(.+){2,}\.(.+){2,}/;

After the DOM finishes loading we need to check for keyup events only inside the #email input. But before this event handler I’ve coded a regex variable which we’ll test against the user’s input value. Following the keyup event we can setup another variable which holds the current text inside #email.

Gravatar can display images with just a simple URL hack by creating an MD5 hash of the e-mail address. This will in turn look something like If you need an image value returned you may append the .jpg file extension. But in this example I’m setting a specific size value so we get 120×120 images.

Calling an Ajax Request

We need some logic to check each time the user enters a key if their current e-mail is valid. If so we then need to pass that data into PHP so we can get back an MD5 hash, and thus display a new Gravatar image. Here is what the rest of our jQuery code looks like:

		if(regex.test(addr)) {
				type: "POST",
				url: "md5.php",
				data: "email="+addr,
				success: function(html){
					var newimg = ""+html+"?s=120";
					$("#userimg").attr("src", newimg)

Remember that regex is referring to the variable I setup earlier. This contains a series of complex code syntax to determine if the e-mail text has an @ symbol in the middle, along with some TLD suffix(.co, .com, .net, If you’re interested to learn more there is great documentation on the JavaScript .test() method at W3Schools.

But in short, this function will only return true if the e-mail address matches up correctly. If true then we call a new Ajax method which connects into a backend script md5.php. We’re passing a new variable which contains the current e-mail text entered by the user. Then inside the return function we’re appending the new MD5 hash to the Gravatar URL and updating the top Gravatar image src attribute dynamically.

Final Bits of PHP

Now that all the basic frontend jargon has been explained we should take a peek behind the scenes. All we’re doing inside PHP is grabbing the user’s e-mail, creating an MD5 hash string, and returning the value back to jQuery. Below I’ve copied everything from the md5.php file:

$email = trim(strtolower($_POST['email']));

$md5 = md5($email);
echo $md5;

In the first line I’m running some functions on our dynamic variable $_POST[‘email’]. This is passed over via jQuery, so I’m stripping out any excess whitespace and also forcing all letters into lower case. Then with a simple call to the PHP md5() function we can get our string value and echo it onto the document, which in this case returns the value back to jQuery.

Live DemoDownload Source Code


This is such a unique effect which can work well on nearly any website. WordPress or Joomla! based CMS websites running many users can enjoy the benefits, although mostly aesthetic, each time you log in. And there are plenty of other scenarios where you can pull a user’s gravatar image dynamically for some cool effects. Download a copy of my demo source code to play around with, and feel free to share your thoughts in the post discussion area below.

About the author:

Jake is a creative designer, illustrator, and web developer. He frequently writes articles involving new-age design concepts and freelance management skills. You can find him in Google or follow his tweets @jakerocheleau


Scroll back to the top