Tauquil’s Blog | The Bugs are Haunting Me
Scary stuff going on here!
Thu 22nd Dec 2005
I wrote a Gravatar modifier for Smarty:
{$comment->comment_author_email|gravatar:"default":
size:"rating":border}
For example, to use no default image, and limit the size to 40×40 pixels, you’d use:
{$comment->comment_author_email|gravatar:"":"40":"":""}
Notice the double quotes where the argument is missing, and that (in this version) you actually need to include all of the arguments.
(I’ve updated this, but haven’t tested it fully yet).
Anyway, here’s the code:
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty gravatar plugin
*
* Type: modifier<br>
* Name: gravatar<br>
* Author: Matt Schinckel<br>
* mailto:matt@schinckel.net<br>
* aim:mschinckel<br>
* http://schinckel.net<br>
* Purpose: convert email address to gravatar
* @param string
* @return string
*/
function smarty_modifier_gravatar($email, $default=false, $size=false, $rating=false, $border=false)
{
$gravurl = "<img src='http://www.gravatar.com/avatar.php?gravatar_id=".md5($email);
if ($default)
{
$gravurl = $gravurl."&default=".urlencode($default);
}
if ($size)
{
$gravurl = $gravurl."&size=".$size;
}
if ($rating)
{
$gravurl = $gravurl."&rating=".$rating;
}
if ($border)
{
$gravurl = $gravurl."&border=".$border;
}
return $gravurl."' alt='Gravatar Image' />";
}
?>