Welcome Guest, Not a member yet? Register   Sign In
Helper: Gravatar (Revised)

[eluser]David Cassidy[/eluser]

The purpose of the gravatar helper is to help you quickly and easily implement gravatars on your own website by constructing the 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.


1. Unpack the contents of the archive to an easily accessible directory, leaving the path names intact.

2. Copy the contents of the upload directory to your web server where CodeIgniter is installed, taking special care to preserve the directory structure.


Using the gravatar helper in your applications is very simple and straightforward. To begin using it, add the following code to your controller:
$this->load->helper( 'gravatar' );

Next, add the following bit of code to your view where you would like to display the gravatar:
<?php echo gravatar( "[email protected]" ); ?>

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.

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.

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 down-sampled using bicubic resampling before output.

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.
<?php echo gravatar( "[email protected]", "PG", "40", "http://www.domain.com/default.gif" ); ?>

In turn, the results would be:

Typically, you should use the examples above to construct an <IMG> element to display a user's gravatar like so:
<img src="&lt;?php echo gravatar( "[email protected]" ); ?&gt;" alt="Gravatar">


Version 1.0
Release Date: Wednesday, August 08, 2007

* Reformulated code to generate URLs only, omitting the markup.
* Created user documentation.

Version 1.0 Beta
Release Date: Sunday, July 08, 2007


Unix/Linux Users:

Windows Users:

[eluser]Phil Sturgeon[/eluser]
Good stuff, even though the code is simple it is well packaged and well documented. Will be using it, thanks.

[eluser]David Cassidy[/eluser]
[quote author="thepyromaniac" date="1183941390"]Good stuff, even though the code is simple it is well packaged and well documented. Will be using it, thanks.[/quote]


Anywhere there is the need for an avatar, I prefer to use gravatars. This way I can encourage the use of their services to ensure they'll be around for awhile through use and advertisement. But this aside, it's also much easier to fetch their gravatar than it is to create an avatar storage system and maintain the users' avatars locally (not that it is hard). Gravatar is a great service.

I've written my own but it's too messy to be posted, so just on suggestion, you should provide a method (or public member) to return just the image url, so that way you can be more flexible when using it, background image for example.

[eluser]David Cassidy[/eluser]
It would probably just be best to omit all of the markup. I've noticed a few issues myself. Issues like the fact that this assumes you are using XHTML instead of HTML. I've already gone the route of stripping out the markup myself.

Updated the code above in the initial post. Thanks for all your suggestions.

[eluser]David Cassidy[/eluser]
The above documentation and the source is now also available on the CodeIgniter wiki: http://codeigniter.com/wiki/Gravatars/

Before anyway says, I know it's four years old!

Anyway, I wanted a to retrieve Gravatar images with ease, this function did what I wanted it to do.. However I did find it to be a bit restrictive..

The function I created is basically the same, with a few edits and a more appropriate function name I believe.
function gravatar_img($email, $return_img = TRUE, $default = 'mm', $size = 80, $rating = 'PG', $force_default = FALSE)

    $email_hash = md5(strtolower(trim($email)));
    $fd = ($force_default === TRUE) ? '&amp;forcedefault=y' : '';
    $image_url = (isset($_SERVER['HTTPS']) ? 'https://secure.' : 'http://') .
        $email_hash .
        '?size='. $size .
        '&amp;default='. $default .
        '&amp;rating='. $rating . $fd;
    return $return_img === TRUE ? '<img src="'. $image_url .'" class="gravatar_image" />' : $image_url;

Is there any way to use a cache to not take so long to load the web?

if I have to load 100 avatars is horrible

Theme © iAndrew 2016 - Forum software by © MyBB