Creating the Hash
All URLs on Gravatar are based on the use of the hashed value of an email address. Images and profiles are both accessed via the hash of an email, and it is considered the primary way of identifying an identity within the system. To ensure a consistent and accurate hash, the following steps should be taken to create a hash:
- Trim leading and trailing whitespace from an email address
- Force all characters to lower-case
- md5 hash the final string
As an example, let's say we start with "MyEmailAddress@example.com " (note the trailing space which our hypothetical user entered by mistake). If we md5 encode that string directly, we get the following (in PHP):
echo md5( "MyEmailAddress@example.com " ); // "f9879d71855b5ff21e4963273a886bfc"
If we now run that same email address through the above process, you will see that we get a different result (again in PHP):
$email = trim( "MyEmailAddress@example.com " ); // "MyEmailAddress@example.com" $email = strtolower( $email ); // "myemailaddress@example.com" echo md5( $email ); // "0bc83cb571cd1c50ba6f3e8a78ef1346"
This can easily be combined into a single line:
echo md5( strtolower( trim( "MyEmailAddress@example.com " ) ) );
Once you have generated a consistent hash, you can then request either an image or a profile.