SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

Code a Dynamic Photo Login Box using Gravatar

July 23, 2012 by Jake Rocheleau

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 Demo – Download 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="http://www.gravatar.com/avatar/00000000000000000000000000000000?s=120" alt="Gravatar" id="userimg">
		</div>
			
		<div class="input-field">
			<label for="email">e-mail</label>
			<input type="email" name="email" id="email" class="basic">
		</div>
	
		<div class="input-field">
			<label for="password">password</label>
			<input type="password" name="password" id="password" class="basic">
		</div>
			
		<input type="submit" id="formsub" value="Login!">
	</form>
</div>

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.

$(document).ready(function(){
	
	$("#email").keyup(function(){
		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 http://www.gravatar.com/avatar/MD5HASH. 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)) {
			$.ajax({
				type: "POST",
				url: "md5.php",
				data: "email="+addr,
				success: function(html){
					var newimg = "http://www.gravatar.com/avatar/"+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, .co.uk). 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:

<?php
$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 Demo – Download Source Code

Conclusion

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.

Filed Under: Tutorial Tagged With: ajax, avatar, form, gravatar, login, photo, wordpress

Recent Posts

  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022
  • How to Get Your First Web Design Client

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022