📜 ⬆️ ⬇️

Smarty plugin for Gravatar

Many people know about the popular service - Gravatar . Let me briefly remind you of its essence - anyone can register there, upload their avatar and link it with their e-mail address. Gravatar service provides an opportunity to get an avatar of any registered user. This plugin allows you to build an address for the user's avatar in Smarty templates.

Briefly on how to build a URL for Gravatar. It looks like:
  http://www.gravatar.com/avatar/ <email_hash>? s = <size> & r = <rating> & d = <default> 
<email_hash> - MD5 hash from a lower-case e-mail address with no spaces at the beginning and end;
<size> is the size of the avatar in pixels, from 1 to 512;
<rating> - avatar rating, accepts only the following values: “g”, “pg”, “r” or “x”;
<default> - the avatar address for those users who are not registered in the Gravatar service. URL must be encoded with urlencode.

The plugin itself looks like this:
<? php
/ *
* Smarty plugin
* ------------------------------------------------- ------------
* File: function.gravatar.php
* Type: function
* Name: gravatar
* Purpose: Build a URL for Gravatar
* ------------------------------------------------- ------------
* /
function smarty_function_gravatar ($ params )
{
$ url = 'http://www.gravatar.com/avatar/' ;

if (empty ($ params [ 'email' ])) {
$ params [ 'email' ] = '' ;
}
$ url. = md5 (strtolower (trim ($ params [ 'email' ])));
')
$ firstparam = true ;
if (isset ($ params [ 'size' ])) {
$ url. = '? s =' . $ params [ 'size' ];
$ firstparam = false ;
}

if (isset ($ params [ 'rating' ])) {
$ url. = ($ firstparam)? '?' : '&' ;
$ url. = 'r =' . $ params [ 'rating' ];
$ firstparam = false ;
}

if (isset ($ params [ 'default' ])) {
$ url. = ($ firstparam)? '?' : '&' ;
$ url. = 'd =' .urlencode ($ params [ 'default' ]);
}
return $ url;
}
?>

* This source code was highlighted with Source Code Highlighter .

Examples of using:
< img src = "{gravatar email='my@email.com '}" alt = "Gravatar" />

< img src = "{gravatar email='my@email.com 'size = '40'}" alt = "Gravatar" />

< img src = "{gravatar email='my@email.com 'rating =' g '
default = 'http: //www.example.com/avatar.jpg'} " alt =" Gravatar " />

* This source code was highlighted with Source Code Highlighter .


Useful links:
www.gravatar.com - for more information about the service on the official website
www.smarty.net/manual/ru/plugins.php - Smarty Documentation. Creating plugins

Source: https://habr.com/ru/post/40336/


All Articles