For a Cakephp site i had to insert some gravatar images for the comments, so i made a gravatar helper.
Download it here: http://blog.matsimitsu.nl/wp-content/uploads/2007/03/gravatar.rar
and put this code in /app/views/helpers/
Helper code:
<?php
class GravatarHelper extends Helper {
var $helpers = array('Html');
var $grav_url = "http://www.gravatar.com/avatar.php?"; // Url to gravatar service
var $default = "http://dailypicture.nl/img/gravatar.jpg"; // Url to the default picture (ie if the user has no gravatar)
/**
* Retrieves an gravatar image and returns an image tag
*
* @param string $email address used for gravatar generation
* @param integer $size image size
* @param array $htmlAttributes Array of HTML attributes.
* @param boolean $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return mixed Either string or echos the value, depends on AUTO_OUTPUT and $return.
* @access public
*/
function image($email, $size, $htmlAttributes = array(), $return = false) {
$gravatar_url = $this->grav_url."gravatar_id=".md5($email)."&default=".urlencode($this->default)."&size=".$size;
return $this->output(sprintf($this->Html->tags['image'], $gravatar_url, $this->Html->parseHtmlOptions($htmlAttributes, null, '', ' ')), $return);
}
}
?>
In the controller make shure you have the helper enabled:
var $helpers = array('Gravatar');
And to use it in your view :
<?php echo $gravatar->image('john@doe.com', 40, array('alt'=>'gravatar image'), false); ?>
And there you have it, a nice looking gravatar.