 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
01-08-2008, 07:54 PM
|
#1 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
|
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.
__________________
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 03:35 PM.
|
|
|
|
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
|
|
01-09-2008, 02:34 AM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
|
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.
__________________
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.
|
|
|
|
The Following User Says Thank You to Wildhoney For This Useful Post:
|
|
06-05-2008, 03:15 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
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.
|
|
|
|
06-05-2008, 04:21 PM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
|
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.
|
|
|
06-05-2008, 04:42 PM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
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!
|
|
|
|
06-05-2008, 04:49 PM
|
#6 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
...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...
|
|
|
|
06-14-2008, 05:22 AM
|
#7 (permalink)
|
|
The Contributor
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 50
Thanks: 3
|
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.
|
|
|
|
|
The Following User Says Thank You to ryanmr For This Useful Post:
|
|
06-14-2008, 08:57 AM
|
#8 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
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.
|
|
|
|
|
The Following User Says Thank You to ciprianmp For This Useful Post:
|
|
06-14-2008, 09:00 AM
|
#9 (permalink)
|
|
The Contributor
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 50
Thanks: 3
|
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).
|
|
|
|
|
The Following User Says Thank You to ryanmr For This Useful Post:
|
|
06-14-2008, 10:14 AM
|
#10 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
Quote:
Originally Posted by ryanmr
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!
|
|
|
|
06-14-2008, 08:53 PM
|
#11 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
I just found a typo Ryan... on line 50:
PHP Code:
private $defult;
"defult" should be "default"
|
|
|
|
|
The Following User Says Thank You to ciprianmp For This Useful Post:
|
|
06-15-2008, 09:34 PM
|
#12 (permalink)
|
|
The Contributor
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 50
Thanks: 3
|
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.
|
|
|
|
|
The Following User Says Thank You to ryanmr For This Useful Post:
|
|
06-15-2008, 09:47 PM
|
#13 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
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
|
|
|
|
06-15-2008, 09:52 PM
|
#14 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
Quote:
Originally Posted by ryanmr
Thanks! I knew I made some mistakes. :)
|
private $defult; is still there Ryan ;)
|
|
|
|
06-15-2008, 11:40 PM
|
#15 (permalink)
|
|
The Contributor
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 50
Thanks: 3
|
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.
|
|
|
|
06-16-2008, 10:08 PM
|
#16 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
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?
|
|
|
|
06-16-2008, 10:18 PM
|
#17 (permalink)
|
|
The Contributor
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 50
Thanks: 3
|
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.
|
|
|
|
|
The Following User Says Thank You to ryanmr For This Useful Post:
|
|
06-17-2008, 05:27 PM
|
#18 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
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
|
|
|
|
06-17-2008, 05:47 PM
|
#19 (permalink)
|
|
The Contributor
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 50
Thanks: 3
|
Glad I could help out!
|
|
|
|
06-18-2008, 12:41 PM
|
#20 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Bucharest, Romania
Posts: 14
Thanks: 6
|
phpMyChat-Plus - first Gravatarized LiveChat System!!!
Quote:
Originally Posted by ciprianmp
...
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
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|