Adobe Flex Gravatar Control

Flex Gravatar Control

I have recently been getting to grips with Adobe Flex for a project that I’ve been working on, and thought a fun little bit of work would be to write a Flex Gravatar Control. I’d done the equivalent for ASP.NET, so why not have another go?

Incidentally, I don’t know whether there’s much call for a Flex Gravatar control – if you’re reading this and find a use for it – great, but I hope that there may be something in this post that helps a budding Flex developer too.

The general idea of component development is to package up a reusable bit of code for use in applications. Since a Gravatar is just an image, it seems natural that the Gravatar component extends (otherwise known as inheriting) the Flex mx.controls.Image class. The benefit of inheritance here is that we get a bunch of functionality for free that we can use as a basis for our control. We’ll be adding our specific Gravatar code so that the image shown is a particular user’s Gravatar.

Let’s take a look at the Flex Navigator for the Gravatar Project.

Flex Navigator for the Gravatar Project
Flex Navigator for the Gravatar Project

Aside from the standard stuff, as3corelib.swc is referenced in the libs folder. If you’re not aware of it, as3corelib is described as follows over at the project page hosted on Google Code.

The corelib project is an ActionScript 3 Library that contains a number of classes and utilities for working with ActionScript 3. These include classes for MD5 and SHA 1 hashing, Image encoders, and JSON serialization as well as general String, Number and Date APIs.

Since Gravatars use hashing for their URLs, we’ll be using the MD5 aspect of corelib.

The components directory contains a single file,, which contains the code for the Gravatar component, and Gravatar.mxml is the demo application’s MXML that uses the Gravatar component.

Let’s dive straight in and take a look at the component code:

package components
	// import the MD5 hashing code:
	import com.adobe.crypto.MD5;
	import mx.controls.Image;
	public class GravatarImage extends Image
		public function GravatarImage()
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			this.width = _size;
			this.height = _size;
			this.source = "" +
							MD5.hash(_email).toString() + "?" +
							"size=" + _size +
							"&rating=" + _rating +
							"&d=" + _defaultImage;
		private var _validRatings:Array = new Array('g', 'pg', 'r', 'x');
		private var _validDefaultImageTypes:Array = new Array('default', 'identicon', 'monsterid', 'wavatar');
		private var _email:String = new String();
		private var _size:Number = 80;		// default size of 80 pixels
		private var _rating:String = "G";	// default rating of G
		private var _defaultImage:String = "default"; // default to the blue 'G'
		public function set email(value:String):void
			// if the email is invalid, a default image will be returned:
			_email = value;
		public function set size(value:Number):void
			// sanity check on incoming value, must be between 1 and 512:
			if( value >= 1 && value < 512)
				_size = value;
				_size = 80;
		public function set rating(value:String):void
			// do a sanity check on the rating, allowing values
			// only in the _validRatings array (defined above):
			if( _validRatings.indexOf( value.toLowerCase()) != -1)
				_rating = value;
		public function set defaultImage(value:String):void
			_defaultImage = value;

The code is pretty simple. The main function, updateDisplayList is called when the image should draw itself. It simply constructs a URL based on the properties that have been set, and makes a request to the gravatar service for the image. Note that the corelib’s MD5 hash is used to obfuscate the email address passed in.

The properties relate to those of the URL generated and are as follows:


The email address associated with the gravatar image.


The size property of the control can be in the range 1 to 512. If it is outside this range, a default of 80 will be used.


The ‘highest’ allowed rating of image.

  • A G rated gravatar is suitable for display on all websites with any audience type.
  • PG rated gravatars contain may contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence.
  • R rated gravatars may contain such things as harsh profanity, intense violence, nudity, or hard drug use.
  • X rated gravatars may contain hardcore sexual imagery or extremely disturbing violence.


URL encoded URL, protocol included, of a GIF, JPEG, or PNG image that should be returned if either the requested email address has no associated gravatar, or that gravatar has a rating higher than is allowed by the ‘MaxAllowedRating’ property. The default image type also supports ‘default’, ‘identicon’, ‘monsterid’, and ‘wavatar’ strings for alternative Gravatar default images.

So, how do we use the control? Again, it’s pretty simple. Let’s have a look at the application MXML:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" layout="absolute"
			private var postComments:XML =
                    <comment name="Shane Porter" email="anemail@somedomain" />
					<comment name="Kate" email="someotheremail@somedomain" />
					<comment name="Mr Unknown" email="" />
	<mx:DataGrid id="myDataGrid" dataProvider="{postComments.comment}" rowHeight="88" rowCount="3">
            <mx:DataGridColumn dataField="@name" headerText="Name"/>
            <mx:DataGridColumn dataField="@email" headerText="Gravatar">
              			<components:GravatarImage email="{data.@email}" size="80" rating="G" defaultImage="monsterid" />

There’s a component namespace reference on the outer Application element, and for illustrative purposes, some XML defined that is used as the data provider for a Data Grid control. The GravatarImage component is used as an item renderer for the ‘Gravatar’ column. The email property is set to the value of the email XML attribute, and the size, rating and defaultImage is also set.

Application in Design View
Application in Design View

When the app is run, the datagrid uses the XML as its data provider, and the Gravatars are rendered. Note that in this screenshot, I provided known email addresses associated with Gravatars. The bottom row is the same as the code shown previously, and since it is not associated with a Gravatar, the specified ‘monsterid’ default image is used.

The page shown in the browser
The page shown in the browser