Gravatar Helper
A gravatar, or globally recognized avatar, is quite simply an avatar image that follows you from weblog to weblog appearing beside your name when you comment on gravatar enabled sites. Avatars help identify your posts on web forums, so why not on weblogs?
Gravatars are a great addition to any blog, or indeed any user submitted content. This helper is designed to provide gravatar output without the fuss
Introduction
This is my first community posted code. Criticism welcome, but please be kind.
There isn't much to this Helper, so I will jump right in.
The code is available at the end of this article, and is also hosted on Github: http://github.com/predominant/CakePHP-Goodies
Usage
More than likely you just want to use the thing... Well, its as simple as this:
Including the helper in your application
As with any helper, ensure that it is included in the helpers array in your controller. This will allow you access to it when you need it, in your views.
Controller
class MyController extends AppController {
public $helpers = array('Gravatar');
// ...
}
View
Basic Gravatar with default settings:
<?php echo $gravatar->image('someone@cakeisawesome.com'); ?>
Altering the default gravatar:
<?php echo $gravatar->image(
'someone@cakeisawesome.com',
array(
'default' => 'identicon'
); ?>
Altering the default gravatar with a custom image:
<?php echo $gravatar->image(
'someone@cakeisawesome.com',
array(
'default' => 'http://mysite.com/defaultavatar.png'
)); ?>
Changing the gravatar size:
<?php echo $gravatar->image(
'someone@cakeisawesome.com',
array(
'default' => 'identicon',
'size' => 120
); ?>
Adjusting the gravatar ratings to display:
<?php echo $gravatar->image(
'someone@cakeisawesome.com',
array(
'rating' => 'x'
); ?>
Including image filename extension on the generated URL:
<?php echo $gravatar->image(
'someone@cakeisawesome.com',
array(
'default' => 'identicon',
'ext' => true
); ?>
Options
The options and their valid values are as follows:
default
- 'identicon' (sample: http://www.gravatar.com/avatar/3b3be63a4c2a439b013787725dfce802?d=identicon)
- 'monsterid' (sample: http://www.gravatar.com/avatar/3b3be63a4c2a439b013787725dfce802?d=monsterid)
- 'wavatar' (sample: http://www.gravatar.com/avatar/3b3be63a4c2a439b013787725dfce802?d=wavatar)
- URL to image (example: http://mysite.com/defaultavatar.png)
size
- Minimum value 1
- Maximum value 512
rating
- 'g'
- 'pg'
- 'r'
- 'x'
ext
- true
- false
Helper Code
Helper Class:
<?php
<?php
App::import(array('Security', 'Validation'));
/**
* CakePHP Gravatar Helper
*
* A CakePHP View Helper for the display of Gravatar images (http://www.gravatar.com)
*
* @copyright Copyright 2010, Graham Weldon
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @package goodies
* @subpackage goodies.tests.cases.helpers
*
*/
class GravatarHelper extends AppHelper {
/**
* Gravatar avatar image base URL
*
* @var string
* @access private
*/
private $__url = array(
'http' => 'http://www.gravatar.com/avatar/',
'https' => 'https://secure.gravatar.com/avatar/'
);
/**
* Hash type to use for email addresses
*
* @var string
* @access private
*/
private $__hashType = 'md5';
/**
* Collection of allowed ratings
*
* @var array
* @access private
*/
private $__allowedRatings = array('g', 'pg', 'r', 'x');
/**
* Default Icon sets
*
* @var array
* @access private
*/
private $__defaultIcons = array('none', 'identicon', 'monsterid', 'wavatar', '404');
/**
* Default settings
*
* @var array
* @access private
*/
private $__default = array(
'default' => null,
'size' => null,
'rating' => null,
'ext' => false);
/**
* Helpers used by this helper
*
* @var array
* @access public
*/
public $helpers = array('Html');
/**
* Constructor
*
* @access public
*/
public function __construct() {
// Default the secure option to match the current URL.
$this->__default['secure'] = env('HTTPS');
}
/**
* Show gravatar for the supplied email address
*
* @param string $email Email address
* @param array $options Array of options, keyed from default settings
* @return string Gravatar image string
* @access public
*/
public function image($email, $options = array()) {
$imageUrl = $this->url($email, $options);
unset($options['default'], $options['size'], $options['rating'], $options['ext']);
return $this->Html->image($imageUrl, $options);
}
/**
* Generate image URL
*
* @param string $email Email address
* @param string $options Array of options, keyed from default settings
* @return string Gravatar Image URL
* @access public
*/
public function url($email, $options = array()) {
$options = $this->__cleanOptions(array_merge($this->__default, $options));
$ext = $options['ext'];
$secure = $options['secure'];
unset($options['ext'], $options['secure']);
$protocol = $secure === true ? 'https' : 'http';
$imageUrl = $this->__url[$protocol] . $this->__emailHash($email, $this->__hashType);
if ($ext === true) {
// If 'ext' option is supplied and true, append an extension to the generated image URL.
// This helps systems that don't display images unless they have a specific image extension on the URL.
$imageUrl .= '.jpg';
}
$imageUrl .= $this->__buildOptions($options);
return $imageUrl;
}
/**
* Sanitize the options array
*
* @param array $options Array of options, keyed from default settings
* @return array Clean options array
* @access private
*/
private function __cleanOptions($options) {
if (!isset($options['size']) || empty($options['size']) || !is_numeric($options['size'])) {
unset($options['size']);
} else {
$options['size'] = min(max($options['size'], 1), 512);
}
if (!$options['rating'] || !in_array(mb_strtolower($options['rating']), $this->__allowedRatings)) {
unset($options['rating']);
}
if (!$options['default']) {
unset($options['default']);
} else {
if (!in_array($options['default'], $this->__defaultIcons) && !Validation::url($options['default'])) {
unset($options['default']);
}
}
return $options;
}
/**
* Generate email address hash
*
* @param string $email Email address
* @param string $type Hash type to employ
* @return string Email address hash
* @access private
*/
private function __emailHash($email, $type) {
return Security::hash(mb_strtolower($email), $type);
}
/**
* Build Options URL string
*
* @param array $options Array of options, keyed from default settings
* @return string URL string of options
* @access private
*/
private function __buildOptions($options = array()) {
$gravatarOptions = array_intersect(array_keys($options), array_keys($this->__default));
if (!empty($gravatarOptions)) {
$optionArray = array();
foreach ($gravatarOptions as $key) {
$value = $options[$key];
$optionArray[] = $key . '=' . mb_strtolower($value);
}
return '?' . implode('&', $optionArray);
}
return '';
}
}
?>
?>
Final Note
My final note is with regard to the options provided to the helper. Given that the HTML helper already deals extensively with images, it is used to process the actual image tage and return it. Thus, providing any Html Helper image options will ensure they are passed through the gravatar component and onto the Html component, rendering as you would naturally expect from the core Html helper.
Comments and suggestions are encouraged.
If you are using this on your site, let me know!
Code also available on Github: http://github.com/predominant/CakePHP-Goodies








I wait behind the visit … Techno News
I have integrated into my CakePHP app.
Regards
The-Di-Lab
www.thedilab.com
www.startutorial.com
If you have any suggestions or improvements, please let me know.
Thanks!
you would be able to check on whether an email has an account or get all pics of that account etc
just wondering
would be cool to validate that first before a user can add his gravatar
It struck me that you should send a link to the gravatar team, and hopefully they will add it to their implementation guide on the page.
Good luck and thanks for a good helper.
Just ask google about 'gravatar cakephp' and what pops up if not the perfect solution.
This doesnt affect the previous functionality at all.
Thanks to Mark Scherer for various updates and contributions.
Great work Graham!
Thanks for the suggestions and help with this helpers code. And enjoy the updates.
You should update your code to reflect Tyler's suggestion about encoding the URL properly.
Also, your hashing method is not taking into account the fact that Gravatar is expecting the email to be hashed in its lower-case form. Hence, the following change should be applied...
Change this:
Helper Class:
<?phpreturn Security::hash($email, $type);
?>
To this:
Helper Class:
<?phpreturn Security::hash(strtolower($email), $type);
?>
That's all there is to it. :)
I'm a newbe and would like to know how to place this code in a html page?
Do I need a seperate php page with script for each persons Gravatar?
Do I place the script like this below directly into the html page??
image(
'someone@cakeisawesome.com',
array(
'rating' => 'x'
); ?>
You actually spotted an error in the code samples there, as there is a missing close bracket, but you can place this into any view .ctp file in your CakePHP Application / Plugin.
The simplest and easiest way to display a gravatar is to do:
View Template:
echo $gravatar->image('someone@cakeisawesome.com');
And just ensure you include the Gravatar helper in your controller (or AppController), like this:
Controller Class:
<?phppublic $helpers = array('Html', 'Gravatar');
?>
$optionString = implode('&', $optionArray);To:
$optionString = implode('&', $optionArray);Comments are closed for articles over a year old