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.