Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 14 revisions

Category: Helpers

[size=4]Introduction[/size]

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.

[size=4]Source[/size] [pre][code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**

/**

  • CodeIgniter Gravatar Helper
  • @package CodeIgniter
  • @subpackage Helpers
  • @category Helpers
  • @author David Cassidy */

/**

  • 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, $rating = 'X', $size = '80', $default = 'http://gravatar.com/avatar.php' ) {

    Hash the email address

    $email = md5( $email );

    Return the generated URL

    return "http://gravatar.com/avatar.php?gravatar_id=" .$email."&rating=" .$rating."&size=" .$size."&default=" .$default; }

?> [/code][/pre]

[size=4]Usage[/size]

Using the gravatar helper in your applications is very simple and straightforward. To begin using it, add the following code to your controller:

[code]$this->load->helper( 'gravatar' );[/code]

Next, add the following bit of code to your view where you would like to display the gravatar:

[code]<?php echo gravatar( "you@domain.com" ); ?>[/code]

The above, will load the gravatar for the specified email address. Optionally, you may want to specify some additional parameters to impose limitations on the gravatar image.

[b]Rating[/b] 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.

[b]Size[/b] 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.

[b]Default[/b] 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.

Below is an example illustrating the use of these optional parameters.

[code]<?php echo gravatar( "you@domain.com", "PG", "40", "http://www.domain.com/default.gif" ); ?>[/code]

In turn, the results would be:

[code]http://gravatar.com/avatar.php?gravatar_id=0ee5f81c11737062773c780c611ab0d5&rating=PG&size=40&default=http://www.domain.com/default.gif[/code]

Typically, you should use the examples above to construct an element to display a user's gravatar like so:

[code]<img src="<?php echo gravatar( "you@domain.com" ); ?>" alt="Gravatar">[/code]

Clone this wiki locally