PHP Classes

File: GravatarUrlGenerator.php

Recommend this page to a friend!
  Classes of Stanislav Shramko   Gravatar URL Provider   GravatarUrlGenerator.php   Download  
File: GravatarUrlGenerator.php
Role: Class source
Content type: text/plain
Description: Generator class
Class: Gravatar URL Provider
Generate URLs for user images in Gravatar.com
Author: By
Last change: Added the default pic.
Date: 14 years ago
Size: 1,741 bytes
 

Contents

Class file image Download
<?php
/**
 * Generates URLs for Gravatar.com service using e-mails. Also caches the results.
 *
 * @author Stanis Shramko <mailto:im@dinexi.ru>
 * @version 1.0
 * @license LGPL
 * @link http://www.gravatar.com
 * @link http://webfari.ru
 * @copyright Webfari
 */
class GravatarUrlGenerator {

   
/**
     * Keeps the URL of Gravatar script
     */
   
const GRAVATAR_URL = 'http://www.gravatar.com/avatar.php';
   
   
/**
     * Contains the possible ratings
     *
     * @var array
     */
   
private static $GRAVATAR_RATINGS = array('G', 'PG', 'R', 'X');
   
   
/**
     * Keeps the URLs for Gravatars in the simple hash structure; acts as a cache
     *
     * @var array
     */
   
private static $gravatarsCache = array();
   
   
/**
     * Returns the Gravatar URL
     *
     * @param $email string email
     * @param $rating rating from GRAVATAR_RATINGS
     * @param $size integer means the size
     * @param $default string default URL if there is no pic
     * @param $useCache boolean to cache the results or not
     * @return string the URL
     */
   
public function generateUrl($email, $rating = "G", $size = 80,
     
$default = 'http://en.gravatar.com/images/gravatars/no_gravatar.gif', $useCache = true)
    {
       
$email = md5(strtolower($email));
       
$rating = in_array($rating, self::$GRAVATAR_RATINGS) ? $rating : self::$GRAVATAR_RATINGS[0];
       
$size = (int)$size;
        if (
$useCache) {
            if (isset(
self::$gravatarsCache[$email][$rating . $size])) {
                return
self::$gravatarsCache[$email][$rating . $size];
            }
        }
       
$args = array(
           
'gravatar_id' => $email,
           
'default' => $default,
           
'size' => $size,
           
'rating' => $rating
       
);
       
$url = self::GRAVATAR_URL . '?' . http_build_query($args);
        if (
$useCache) {
           
self::$gravatarsCache[$email][$rating . $size] = $url;
        }
        return
$url;
    }
   
}

?>