How use Gravatar in your Website

Written by  Sunday, 19 April 2015 18:37

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 " ) ) );

In addition to allowing you to use your own image, Gravatar has a number of built in options which you can also use as defaults. Most of these work by taking the requested email hash and using it to generate a themed image that is unique to that email address. To use these options, just pass one of the following keywords as the d= parameter to an image request:

  • 404: do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response
  • mm: (mystery-man) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)
  • identicon: a geometric pattern based on an email hash
  • monsterid: a generated 'monster' with different colors, faces, etc
  • wavatar: generated faces with differing features and backgrounds
  • retro: awesome generated, 8-bit arcade-style pixelated faces
  • blank: a transparent PNG image (border added to HTML below for demonstration purposes)

Screen Shot 2015 04 19 at 10.42.17 PM 2e6f2

If for some reason you wanted to force the default image to always load, you can do that by using the f= or forcedefault= parameter, and setting its value to y.

Gravatar allows users to self-rate their images so that they can indicate if an image is appropriate for a certain audience. By default, only 'G' rated images are displayed unless you indicate that you would like to see higher ratings. Using the r= or rating= parameters, you may specify one of the following ratings to request images up to and including that rating:

  • g: suitable for display on all websites with any audience type.
  • pg: may contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence.
  • r: may contain such things as harsh profanity, intense violence, nudity, or hard drug use.
  • x: may contain hardcore sexual imagery or extremely disturbing violence.

http://www.gravatar.com/avatar/5a73cba6c0bb263e46d2710f4702d600?s=48&r=pg&s=200

s=48 this is size 48px by 48px

1012 Read Last modified on Sunday, 19 April 2015

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.