-
Notifications
You must be signed in to change notification settings - Fork 4
Gravatars
Category: Helper::Community | Category:Helper::Gravatars
The purpose of the gravatar helper is to help you quickly and easily implement gravatars on your own website by constructing the Uniform Resource Identifier (URI) that is necessary to display the user's gravatar. This simple guide will assist you in installing the gravatar helper as well as illustrate its use.
Original author: David Cassidy - Thanks David!
Better handling of various e-mail formats (example@example.com vs Example@Example.com).
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter Gravatar Helper
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author Drew Johnston
* @link http://drewjoh.com
*/
/**
* Gravatar
*
* Fetches a gravatar from the Gravatar website using the specified params
*
* @access public
* @param string
* @param string
* @param integer
* @param string
* @return string
*/
function gravatar( $email, $size = null, $rating = null, $default = 'mm' )
{
# Return the generated URL
# If we are using SSL, get it from a secure URL
# Lowercase, trim and MD5 hash the e-mail
# Only show options if they're set (defaults will be used otherwise)
return ($_SERVER['HTTPS'] ? 'https' : 'http')
.'://gravatar.com/avatar/'
.md5( strtolower( trim( $email )))
.'?'
.($rating != null ? '&r='.$rating : '')
.($size != null ? '&s='.$size : '')
.($default != null ? '&d='.$default : '');
}
/**
* Gravatar_img
*
* Fetches a gravatar from the Gravatar website using the specified params and returns the <img>
*
* @access public
* @param string
* @param string
* @param integer
* @param string
* @return string
*/
function gravatar_img($email, $size = null, $rating = null, $default = 'mm')
{
return '<img src="'.gravatar($email, $size, $rating, $default).'" alt="Gravatar" />';
}
?>
Using the gravatar helper in your applications is very simple and straightforward. To begin using it, add the following code to your controller: php $this->load->helper( 'gravatar' );
Next, add the following bit of code to your view where you would like to display the URL for the gravatar:```php
<?php echo gravatar( "you@domain.com" ); ?>
You can optionally just get the full <img> tag, add the following bit of code to your view where you would like to display the <img> tag with the URL for the gravatar:```php
<?php echo gravatar_img( "you@domain.com" ); ?>
```The above, will return the <img> tag with the URL of the gravatar for the specified email address.
Optionally, you may want to specify some additional parameters to impose limitations on the gravatar image.
#### Size
Defines the desired height and width of the image in pixels, with acceptable values being between 1 and 80. Specifying a size smaller than 80 will result in the original gravatar image to be downsampled using bicubic resampling before output. **Defaults to 80.**
#### Rating
Defines a maximum rating for the image's content. If the owner has flagged the image with a rating higher than that specified, the default image will instead be used. Acceptable values are G, PG, R, or X. **Defaults to "G".**
#### Default
The default parameter allows you to specify the URL to an image that will be used as the default if the query fails to locate a gravatar for the given email address. Additionally, if a user's gravatar exceeds the rating limit defined by the "rating" parameter, this is the image that will instead be shown. **Defaults to: "mm" (user silhouette)**
### Example
Below is an example illustrating the use of these optional parameters.
```php
<?php echo gravatar( "you@domain.com", "40", "PG", "http://www.domain.com/default.gif" ); ?>
In turn, the results would be:```php http://gravatar.com/0ee5f81c11737062773c780c611ab0d5&r=PG&s=40&d=http://www.domain.com/default.gif
Typically, you would use the examples above to construct an <IMG> element to display a user's gravatar like so:```php
<img src="<?php echo gravatar( " alt="Gravatar">
Or if you don't need any fancy formatting of the tag, you can do:```php <?php echo gravatar_img( "you@domain.com" ); ?>