Smarty-plugin for Gravatar

    Many people know about the popular service - Gravatar . Briefly remind its essence - any user can register there, upload their avatar and associate 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 the address for the user avatar in Smarty templates.

    Briefly about how the URL for Gravatar is built. It has the form:
    http://www.gravatar.com/avatar/? s =& r =& d =
    - MD5 hash from an e-mail address in lower case without spaces at the beginning and end;
    - avatar size in pixels, from 1 to 512;
    - avatar rating, takes only the following values: "g", "pg", "r" or "x";
    - avatar address for those users who are not registered in the Gravatar service. URL must be encoded using urlencode.

    The plugin itself looks like this:
    / *
    * Smarty plugin
    * -------------------------------------------- -----------------
    * File: function.gravatar.php
    * Type: function
    * Name:
    gravatar
    * Purpose: Build 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:
    "Gravatar"

    "Gravatar"

    "Gravatar"

    * This source code was highlighted with Source Code Highlighter .


    Useful links:
    www.gravatar.com - more about the service on the official website
    www.smarty.net/manual/ru/plugins.php - Smarty documentation. Plugin creation

    Also popular now: