TalkPHP
 
 
Account Login
Latest Articles
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
» CSRF POST Token Protection
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP
Reply
 
LinkBack (13) Thread Tools Display Modes
Old 01-08-2008, 07:54 PM   13 links from elsewhere to this Post. Click to view. #1 (permalink)
TalkPHP Loves You
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Nottingham
Posts: 1,332
Thanks: 65
Wildhoney is on a distinguished road
Box Gravatar Wrapper Class

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

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 <img> node like the following:

html4strict Code:
<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.
Attached Files
File Type: zip TalkPHP.com_Gravatar.version.1_0.zip (789 Bytes, 13 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Wildhoney : 01-09-2008 at 03:23 AM.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-09-2008, 02:34 AM   #2 (permalink)
TalkPHP Loves You
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Nottingham
Posts: 1,332
Thanks: 65
Wildhoney is on a distinguished road
Default

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.
Attached Files
File Type: zip TalkPHP.com_Gravatar.version.1_1.zip (1.4 KB, 15 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Wildhoney : 01-09-2008 at 03:23 AM.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/script-giveaway/1905-gravatar-wrapper-class.html
Posted By For Type Date
Automattic Acquired Gravatar — WP Assist This thread Refback 01-13-2008 09:01 PM
Gravatar 3.0, Powered by Automattic | SYP This thread Refback 01-13-2008 04:14 AM
ShadowReality This thread Refback 01-10-2008 02:06 PM
GRavatar plugin for WordPress and WordPress-MU | Ring Of Blogs This thread Refback 01-09-2008 09:31 PM
Automattic Acquires Gravatar | CenterNetworks - Web 2.0 News, Opinions and Insights from New York This thread Refback 01-09-2008 08:00 PM
Gravatar: Globally Recognized Avatars » Webmaster-Source This thread Refback 01-09-2008 02:43 PM
Support WordPress by Displaying Gravatars On Your Blog This thread Refback 01-09-2008 12:35 PM
Now Gravatar Powered. | Act of War: Warfare Re-defined This thread Refback 01-09-2008 06:08 AM
» This Site Now Gravatar-Enabled! » DragonFlyEye.Net This thread Refback 01-09-2008 06:05 AM
MyChores Journal » Blog Archive » User icons provided by Gravatar This thread Refback 01-09-2008 05:22 AM
Do you have your gravatar yet ? This thread Refback 01-09-2008 04:12 AM
Automattic Gets Gravatar - and Valuation « Changing Way This thread Refback 01-09-2008 03:31 AM
Automattic Acquired Gravatar — WP Assist This thread Refback 01-09-2008 03:12 AM

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0