CakePHP Gravatar Helper

Gravatars or (Globally Recognised Avatars) are a service used by many blogs and social media sites on the internet. It lets a user upload an avatar (or various avatars, each with their own rating, similar to those of movies)
and have it linked to their email address. So wherever they post comments or connect socially on the internet they will have Gravatar shown instead of a blank image (as long as the site is using the Gravatar service).

As I am building this blog myself I decided to create a Gravatar helper which will help me to post Gravatars next to comments on my blog articles.

Usage

  1. Put gravatar.php in /app/views/helpers
  2. Add 'Gravatar' to $helpers array in each controller which you wish to use it

    e.g. var $helpers = array('Html', 'Javascript', 'Gravatar');
  3. In a view call the image function with an email address of the gravatar any (or no) of the following options, followed by any HTML image tag attributes you wish to have (e.g. alt, style, width, height etc):
    • size (between 1 and 512 pixels)
    • rating ('g', 'pg', 'r' or 'x')
    • default (a default image to be shown if the gravatar cannot be found - either an absolute path to another image or an email of another gravatar )

e.g. $gravatar->image('test@example.com', array('size' => 50, 'rating' => 'r'), array('alt' => 'Sidebar Avatar', 'width' => 50, 'height' => 50));

Download cakephp-gravatar-helper-1.1.zip (<2kB) OR

Pull the latest source from my GitHub project page.

If you would like to know how the code works, or have any comments, suggestions or bug reports please post them in the comments section below.

Updated Oct 2, 2009: current version 1.1