TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (13) Thread Tools Search this Thread Display Modes
Old 01-08-2008, 08:54 PM   13 links from elsewhere to this Post. Click to view. #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Box Gravatar Wrapper Class

Newest Version: 1.2

I made some changes to the way this class works. Although there are some comments below that say Gravatar changed their API somewhat. Though I don't really see where. The changes I made are as follows:

I added 4 functions instead of the global constants which were being used instead. I removed setRating($szArg) and added 4 separate functions:
  • setRatingAsG
  • setRatingAsPG
  • setRatingAsR
  • setRatingAsX

These all work in the same fashion as setRating() described below. I also added 3 functions for changing the default image if no image is available, and no custom image was supplied:
  • setDefaultImageAsIdentIcon
  • setDefaultImageAsMonsterId
  • setDefaultImageAsWavatar

Last of all, I update the example index.php so that it provides a working example.

Download version 1.2 below!

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, 500 views)
File Type: rar TalkPHP.com_Gravatar.version-1.2.rar (801 Bytes, 231 views)
File Type: zip TalkPHP.com_Gravatar.version-1.2.zip (861 Bytes, 1123 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 : 12-05-2008 at 04:35 PM.
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
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
ciprianmp (06-17-2008), iflashlord (05-23-2009)
Old 01-09-2008, 03:34 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
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, 268 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 04: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
The Following User Says Thank You to Wildhoney For This Useful Post:
ciprianmp (06-05-2008)
Old 06-05-2008, 03:15 PM   #3 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Smile

It works great on php5. I would ask... any chance to make it work also on php4 please?
I get:
Parse error: parse error, unexpected T_OBJECT_OPERATOR in /home/ciprian/public_html/plus/whois_popup.php on line 245
where line 245 is:
PHP Code:
-> setSize(C_AVA_WIDTH
Any solution would be greately appreciated!
Ciprian M.

Last edited by ciprianmp : 06-14-2008 at 11:18 PM.
ciprianmp is offline  
Reply With Quote
Old 06-05-2008, 04:21 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I must admit, I don't understand that error. The only thing I can think of is that type-casting wasn't available in PHP 4? It's a while since I used it so I have completely forgotten. Any help would be appreciated.

The function setSize looks like the following:

php Code:
public function setSize($iSize)
{
    $this->m_iSize = (int) $iSize;
    return $this;
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
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 06-05-2008, 04:42 PM   #5 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Default

The object operator of that line is "->" - as described in php manual - so it doesn't make it to the function itself.
Anyway, there are other operators/constants used in a format unsupported in early php versions. That's why I asked if it can be released for php<5 as well... I like the cache function, that's why I'd stick to your class approach, but I have to make it widely work... otherwise it will break my own app layout - which is not my purpose.
I just want to give users an extra-option of gravatars, besides the included or remote avatars already implemented in phpmychat.

Some feedback so far:
1. I also noticed the cache created file doesn't take the right specified size or rating (neither default) - it always take "80".
2. You defined:
PHP Code:
const GRAVATAR_SITE_URL 'http://www.gravatar.com/avatar.php?gravatar_id=%ssize=%sdefault=%srating=%s'
Notice there is no ampersand separator between the link's vars. The fix should be this:
PHP Code:
const GRAVATAR_SITE_URL 'http://www.gravatar.com/avatar.php?gravatar_id=%s&size=%s&default=%s&rating=%s'
< deprecated>But it still gets the regular gravatar of that email, so the vars are not well working in the class functions...
Tested again and the new link format above does fix it
3.Gravatar v3.0 improved/simplified/extended the link to this format:
PHP Code:
const GRAVATAR_SITE_URL 'http://www.gravatar.com/avatar/%s?s=%s&d=%s&r=%s'
(Gravatar - Globally Recognized Avatars)

I am not used to classes too much, so I'd need your expertize to update, fix & extend this class...

Last edited by ciprianmp : 06-14-2008 at 11:18 PM. Reason: Fixed the version 3 link format - tested!
ciprianmp is offline  
Reply With Quote
Old 06-05-2008, 04:49 PM   #6 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Default

...follow-up
I said "Gravatar 3.0 extended..." because rating can take the 'any' value as well, not just the four g, pg, r or x...
ciprianmp is offline  
Reply With Quote
Old 06-14-2008, 05:22 AM   #7 (permalink)
The Contributor
 
ryanmr's Avatar
 
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 44
Thanks: 3
ryanmr is on a distinguished road
Big Grin rewrite

Hey Guys! I used TalkPHP_Gravatar recently in a project of mine. It had some problems since Gravatar change their API. I made my own class based on TalkPHPGravatar.
My class is pretty similar. Just remember if you want caching, you have to make the "gravatar_cache" folder.
Attached Files
File Type: php PHPGravatar.class.php (6.6 KB, 884 views)
ryanmr is offline  
Reply With Quote
The Following User Says Thank You to ryanmr For This Useful Post:
ciprianmp (06-15-2008)
Old 06-14-2008, 08:57 AM   #8 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Help

Hey it looks nice indeed.
I don't think you realy needed this:
} else {
$this->size = 80;
}
in the setSize function, as the gravatar.com will return the "80" size anyway if the var is larger than 512.

Anyway... here are some challenges:
1. can you also think of a php4 compatible class (if possible... with cache option)?
2. can you think of a "gravatar sign-up" class, so the user sign-up directly using a form on your site and not being sent to gravatar.com? (http://txfx.net/code/wordpress/gravatar-signup/ might be a good start, but that plugin is made for WordPress and not Generic )

Last edited by ciprianmp : 06-14-2008 at 09:48 AM.
ciprianmp is offline  
Reply With Quote
The Following User Says Thank You to ciprianmp For This Useful Post:
ryanmr (06-14-2008)
Old 06-14-2008, 09:00 AM   #9 (permalink)
The Contributor
 
ryanmr's Avatar
 
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 44
Thanks: 3
ryanmr is on a distinguished road
Default php4

I'll see what I can do about the php4 class.
What do you mean, sign up class? I think the user has to have avatar at Gravatar.com (avatar/account).
ryanmr is offline  
Reply With Quote
The Following User Says Thank You to ryanmr For This Useful Post:
ciprianmp (06-14-2008)
Old 06-14-2008, 10:14 AM   #10 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Application Go

Quote:
Originally Posted by ryanmr View Post
I'll see what I can do about the php4 class.
What do you mean, sign up class? I think the user has to have avatar at Gravatar.com (avatar/account).
That plugin can open a small form with the email address/password fields so the user can register to gravatar.com directly from that form, and not being sent to gravatar.com.
I don't use WordPress to check that script, there was a blog with that class implemented but cannot find it right now. Anyway, the no 1 problem is more important for me (php4 compatibility)
Thanks!
ciprianmp is offline  
Reply With Quote
Old 06-14-2008, 08:53 PM   #11 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Default

I just found a typo Ryan... on line 50:
PHP Code:
private $defult
"defult" should be "default"
ciprianmp is offline  
Reply With Quote
The Following User Says Thank You to ciprianmp For This Useful Post:
ryanmr (06-15-2008)
Old 06-15-2008, 09:34 PM   #12 (permalink)
The Contributor
 
ryanmr's Avatar
 
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 44
Thanks: 3
ryanmr is on a distinguished road
Default

Thanks! I knew I made some mistakes. :)

I finished the PHP4 class, but the problem is that PHP4 does not have file_get_contents. I don't really understand sockets, so caching isn't going to workout. Maybe someone could contribute some PHP4 code that mimics file_get_contents.

Here are the two files again, PHPGravatar(4).class.php is the PHP4 file.
Attached Files
File Type: php PHPGravatar(4).class.php (4.1 KB, 885 views)
File Type: php PHPGravatar.class.php (6.6 KB, 822 views)
ryanmr is offline  
Reply With Quote
The Following User Says Thank You to ryanmr For This Useful Post:
ciprianmp (06-15-2008)
Old 06-15-2008, 09:47 PM   #13 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Default

I think copy("src_file", "dest_file") should work on php4 for cache...

Also file() is the php4 version of file_get_contents() only that the output is not put into a string - php.net manual for more details/limitations...

Note this:
file_put_contents
Write a string to a file (PHP 5)
int file_put_contents ( string filename, mixed data [, int flags [, resource context]] )
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

Last edited by ciprianmp : 06-15-2008 at 10:01 PM. Reason: Ading some php4 resources for Ryan
ciprianmp is offline  
Reply With Quote
Old 06-15-2008, 09:52 PM   #14 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Default

Quote:
Originally Posted by ryanmr View Post
Thanks! I knew I made some mistakes. :)
private $defult; is still there Ryan ;)
ciprianmp is offline  
Reply With Quote
Old 06-15-2008, 11:40 PM   #15 (permalink)
The Contributor
 
ryanmr's Avatar
 
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 44
Thanks: 3
ryanmr is on a distinguished road
Default

The problem I had with fopen was that when I had to call filesize on a remote file, it would fail. fgets could have worked and I was looking into it, but then you told me about copy and it works great.
I don't have a normal php4 server for testing, so if something doesn't work, let me know.
Attached Files
File Type: php PHPGravatar5.class.php (6.6 KB, 857 views)
File Type: php PHPGravatar4.class.php (6.7 KB, 795 views)
ryanmr is offline  
Reply With Quote
Old 06-16-2008, 10:08 PM   #16 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Default Some feedback

Ok Ryan,
I tested on all versions of php4 - caching only works on php > '4.3.9' (either with copy or get_contents)
So I chose to disable caching for servers < 4.3.9 - but then I discoverded the $gravatar_url is not working, I couldn't get a direct link to the gravatars - it always tries to read from cache. As the chache jpg is not found, it displays my local avatars...

Tested on both php4 and php5 version, if disableCache() is called, the gravatar_url is not used/returned anymore. Can you test this yourself please? I might do something wrong?
ciprianmp is offline  
Reply With Quote
Old 06-16-2008, 10:18 PM   #17 (permalink)
The Contributor
 
ryanmr's Avatar
 
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 44
Thanks: 3
ryanmr is on a distinguished road
Default

PHP Code:
  require_once("PHPGravatar4.class.php");
  
  
$Gravatar = new PHPGravatar();
  
  
$Gravatar->disableCache();
  
  
$Gravatar->setEmail("oh.nobody@gmail.com");
  
  
$Gravatar->setSize(100);
  
$Gravatar->setDefault("http://img0.gmodules.com/ig/images/sprite_arrow_enlarge_max_min_shrink_x_blue.gif");
  
$Gravatar->setRating("pg");

  echo( 
$Gravatar->get() ); 
That is the code I tested with, and it appears to return the proper gravatar url.
ryanmr is offline  
Reply With Quote
The Following User Says Thank You to ryanmr For This Useful Post:
ciprianmp (06-17-2008)
Old 06-17-2008, 05:27 PM   #18 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Images Great resource!

Yes Ryan, sorry... my bad. Direct links work just fine.
To make it work on php4 (no caching) I had to give up on all file() or file_exists() or is_file() conditions to check the remote files existence. Actually, that was the reason I got scared in the previous post.
When caching is disabled, I wanted to prevent the original gravatar link being dead (like when Gravatar - Globally Recognized Avatars server is down). It works fine on php5 but not on php4, so I guess I'll take that risk of having broken gravatars from time to time (on php4 only).

I'll make a few more refinements next days, then I'll post you a link to a "gravatarized" phpMyChat-Plus version ;)

Thank you Wildhoney and Ryan for your very helpful work.

PS: I'm so excited now that things started working as expected!

Last edited by ciprianmp : 09-04-2008 at 12:26 PM. Reason: spelling
ciprianmp is offline  
Reply With Quote
Old 06-17-2008, 05:47 PM   #19 (permalink)
The Contributor
 
ryanmr's Avatar
 
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 44
Thanks: 3
ryanmr is on a distinguished road
Default

Glad I could help out!
ryanmr is offline  
Reply With Quote
Old 06-18-2008, 12:41 PM   #20 (permalink)
The Wanderer
 
ciprianmp's Avatar
 
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
ciprianmp is on a distinguished road
Bricks phpMyChat-Plus - first Gravatarized LiveChat System!!!

Quote:
Originally Posted by ciprianmp View Post
...
I'll make a few more refinements next days, then I'll post you a link to a "gravatarized" phpMyChat-Plus version ;)
...
Ok, I'm back... I think I got it working fine now!
Here are two versions of the chat I'm developin':
1. Without Cache support (php=4.3.11) - Latest Developing version - phpMyChat-Plus
2. With full Cache support (php=5.2.6) - My Local PC Chat - phpMyChat-Plus (might be off-line from time to time as it's just a local pc, which should go to sleep mode at night time)

No registration required to join chat, but to show your registered gravatar.com, you should register to chat so your email can be set.

PS: your reg gravatar will be returned automatically, all you need to do on Chat Registration page is to check the use of Gravatar.

I hope you like it guys ;)
Let me know if any issues encountered while testing. Thanks!

Last edited by ciprianmp : 09-04-2008 at 12:22 PM. Reason: spelling
ciprianmp 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
Untitled document This thread Refback 01-13-2008 10:01 PM
Gravatar 3.0, Powered by Automattic | SYP This thread Refback 01-13-2008 05:14 AM
ShadowReality This thread Refback 01-10-2008 03:06 PM
GRavatar plugin for WordPress and WordPress-MU | Ring Of Blogs This thread Refback 01-09-2008 10:31 PM
Automattic Acquires Gravatar | CenterNetworks - Web 2.0 News, Opinions and Insights from New York This thread Refback 01-09-2008 09:00 PM
Untitled document This thread Refback 01-09-2008 03:43 PM
Support WordPress by Displaying Gravatars On Your Blog This thread Refback 01-09-2008 01:35 PM
Now Gravatar Powered. | Act of War: Warfare Re-defined This thread Refback 01-09-2008 07:08 AM
Untitled document This thread Refback 01-09-2008 07:05 AM
Untitled document This thread Refback 01-09-2008 06:22 AM
Do you have your gravatar yet ? This thread Refback 01-09-2008 05:12 AM
Untitled document This thread Refback 01-09-2008 04:31 AM
Untitled document This thread Refback 01-09-2008 04:12 AM

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

Advanced Search
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 08:57 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design