I realize the need for this when working on an MVC based CMS especially with Smarty engine in it.
Oh and in case you’re wondering why its sounds like the upcoming movie Avatar by James Cameron, I don’t know. What it has to do with internet term ‘avatar‘? I really don’t know. But in ‘www’, Gravatar is a short term for “Globally Recognized Avatar” assigned by a user as his/her default avatar in every places of the internet, same people behind WordPress. There are other similar services too.
So, this plugin is based from its documentation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | /** * Gravatar * * @link http://www.marvinmarcelo.com * * @param array $params * @param Smarty $smarty */ function smarty_function_gravatar($params, &$smarty) { /** * constant $gravatar_host */ $gravatar_host = "http://www.gravatar.com/avatar/"; if ( !isset($params['email']) ) { $smarty->trigger_error("gravatar: email parameter not set"); return; } if ( !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $params['email']) ) { $smarty->trigger_error("gravatar: {$params['email']} is not a valid email."); return; } /** * @link http://en.gravatar.com/site/implement/url */ $hash = strtolower(md5(trim($params["email"]))); $src = $gravatar_host . $hash . ".jpg?"; if ( !isset($params["size"]) || !($params["size"] >= 1 && $params["size"] < =512 )) { $params["size"] = 32; } $src .= "s={$params["size"]}"; if ( !isset($params["default"]) ) { $params["default"] = "identicon"; } $src .= "d={$params["default"]}"; if ( !isset($params["rating"]) ) { $params["rating"] = "G"; } $src .= "r={$params["rating"]}"; $extras = ""; if ( isset($params["class"]) ) { $extras .= " class=\"{$params["class"]}\""; } if ( isset($params["id"]) ) { $extras .= " id=\"{$params["id"]}\""; } /** * @example http://www.gravatar.com/avatar/7023218434f12aee57f4b03454dadcaa?s=32&d=identicon&r=G */ print "<img src=\"$src\" alt=\"{$params["alt"]}\"$extras />"; } |
Simply extract function.gravatar.zip into your Smarty plugin directory. And use it in your template file like this
{gravatar email='someemail@domain.com'}
You’re done!

