• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Helper: Gravatar (Revised)

#1
[eluser]David Cassidy[/eluser]
Introduction

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.

Installation

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.

Usage

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' );

Next, add the following bit of code to your view where you would like to display the gravatar:
Code:
<?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.

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.

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

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.

Below is an example illustrating the use of these optional parameters.
Code:
<?php echo gravatar( "[email protected]", "PG", "40", "http://www.domain.com/default.gif" ); ?>

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

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

Changelog

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

Download

Unix/Linux Users:
ci_helper_gravatar-1.0.tar.gz

Windows Users:
ci_helper_gravatar-1.0.zip

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

#3
[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]

Thanks!

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.

#4
[eluser]marcoss[/eluser]
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.

#5
[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.

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

#7
[eluser]KarlBallard[/eluser]
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.
Code:
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://') .
        'gravatar.com/avatar/'.
        $email_hash .
        '?size='. $size .
        '&amp;default='. $default .
        '&amp;rating='. $rating . $fd;
    
    return $return_img === TRUE ? '<img src="'. $image_url .'" class="gravatar_image" />' : $image_url;
}

#8
[eluser]Unknown[/eluser]
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.