Sunday, 19 April 2015 18:37

How use Gravatar in your Website

Written by
Rate this item
(6 votes)

As an example, let's say we start with " " (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( " " );
// "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( " " ); // ""
$email = strtolower( $email ); // ""
echo md5( $email );
// "0bc83cb571cd1c50ba6f3e8a78ef1346"

This can easily be combined into a single line:

echo md5( strtolower( trim( " " ) ) );

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.

s=48 this is size 48px by 48px

Read 1538 times Last modified on Sunday, 19 April 2015 18:50

Live and Be Free