Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1555 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Gravatar Wrapper Clas

Thread title: Gravatar Wrapper Clas
Closed Thread    
    Thread tools Search this thread Display Modes  
01-09-2008, 04:26 AM
#1
Wildhoney is offline Wildhoney
Wildhoney's Avatar
Status: Request a custom title
Join date: Feb 2006
Location: Nottingham
Expertise:
Software:
 
Posts: 1,648
iTrader: 18 / 95%
 

Wildhoney is on a distinguished road

Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney

  Old  Gravatar Wrapper Clas

Download

All versions may be downloaded at the following location on TalkPHP.com.

Version 1.0 (Other versions below)

This class is merely a wrapper class for Gravatar. It's dead simple to use, as is Gravatar itself.

Implementing

Each class member returns $this and so you may chain the methods. The following is an example of how to use the Gravatar wrapper class:

PHP Code:
$pAvatar = new TalkPHP_Gravatar();
    
echo $pAvatar    ->    setEmail('example@example.com')
                ->    setSize(80)
                ->    setRating(GRAVATAR_RATING_PG)
                ->    getAvatar();
[/highlight]

The above code will simply return you the URL to the avatar on Gravatar. For it to output an image you need to put it in an [b]<img>[/b] node like the following:

[highlight=html4strict]
<img src="<?php echo $pAvatar->getAvatar(); ?>" alt="Gravatar" />
Parameters

There is a function for each parameter that you may set in order to modify the returned image resource. These are as follows:
  • setImage: Sets the default image for if the email addresses specified does not have a Gravatar image. This must be supplied as a FQDN link to an image resource.
  • setEmail: This is the email address which you are attempting to acquire the Gravatar for. This should be a properly formatted email address.
  • setSize: You may set the return size for the Gravatar. The Gravatar documentation recommends anywhere from 1 to 80 pixels square. This function accepts only 1 parameter as all avatars are square.
  • setRating: This function allows you to control the maximum rating of Gravatar that is acquired. There are numerous constants that can be passed into this function. For example, if I set a maximum of PG, then only G and PG images will be returned.

The following is a list of the 4 constants that can be passed into the setRating function:
  • GRAVATAR_RATING_G
  • GRAVATAR_RATING_PG
  • GRAVATAR_RATING_R
  • GRAVATAR_RATING_X

Installation

This is a simple one file script and therefore is lightweight and fast. Your web host must support PHP 5. To get the script working you will need to include the script into your project like so:

PHP Code:
include_once('./TalkPHP_Gravatar.php'); 
You're then good to go! If you have any issues or general queries or feature suggestions, please respond in this thread and I shall do my utmost.

Version 1.1

I actually decided to work on version 1.1 which contains a cache built in. It recognises the avatar sizes, and will download a fresh copy from the Gravatar server when the current avatar expires. This can easily be changed by opening up the TalkPHP_Gravatar_Cache.php file and modifying the class constant at the top. It is currently set at 5 days.

There are only a couple of additions to the above code that you need to concern yourself with - one of which is you now have to include 2 files. I've separated the classes into 2 classes to make them more manageable. The index.php file shows you how to include the second file, but it's nothing complicated in the least:

PHP Code:
include_once('./TalkPHP_Gravatar.php');
include_once(
'./TalkPHP_Gravatar_Cache.php'); 
The functionality still performs exactly the same as version 1.0 above. There is a directory called /cache/ which should be writeable, so don't forget to set the correct permissions on that. Typically 777.

Last but not least, for the more technically minded, I have added an exception for if the cache class is not TalkPHP_Gravatar_Cache. An exception will be thrown if it isn't to save on any problems. As you shouldn't see this error unless you drastically modify the script, there's really no need to put the class instantiation and method calls in a try and catch block, but it's entirely up to you.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed