GravatarLib
GravatarLib is a small library intended to provide easy integration of gravatar-provided avatars.
copyright
(c) 2011 emberlabs.org
license
This library is licensed under the MIT license; you can find a full copy of the license itself in the file /LICENSE
requirements
PHP 5.3.0 or newer hash() function must be available, along with the md5 algorithmusage
We'll assume you're using this git repository as a git submodule, and have it located at includes/emberlabs/GravatarLib/
according to namespacing rules, for easy autoloading.
general example
<?php include __DIR__ . '/includes/emberlabs/GravatarLib/Gravatar.php'; $gravatar = new emberlabsGravatarLibGravatar(); // example: setting default image and maximum size $gravatar->setDefaultImage('mm') ->setAvatarSize(150); // example: setting maximum allowed avatar rating $gravatar->setMaxRating('pg'); $avatar = $gravatar->buildGravatarURL('someemail@domain.com');
setting the default image
Gravatar provides several pre-fabricated default images for use when the email address provided does not have a gravatar or when the gravatar specified exceeds your maximum allowed content rating.
The provided images are 'mm', 'identicon', 'monsterid', 'retro', and 'wavatar'. To set the default iamge to use on your site, use the method emberlabsGravatarLibGravatar->setDefaultImage()
In addition, you can also set your own default image to be used by providing a valid URL to the image you wish to use.
Here are a couple of examples...
$gravatar->setDefaultImage('wavatar');
$gravatar->setDefaultImage('http://yoursitehere.com/path/to/image.png');
WARNING
If an invalid default image is specified (both an invalid prefab default image and an invalid URL is provided), this method will throw an exception of class InvalidArgumentException
.
setting avatar size
Gravatar allows avatar images ranging from 1px to 512px in size -- and you, the developer or site administrator can specify the exact size of avatar that you want.
By default, the avatar size provided is 80px. To set the avatar size for use on your site, use the method emberlabsGravatarLibGravatar->setAvatarSize()
, and specify the avatar size with an integer representing the size in pixels.
An example of setting the avatar size is provided below:
$gravatar->setAvatarSize(184);
WARNING
If an invalid size (less than 1, greater than 512) or a non-integer value is specified, this method will throw an exception of class InvalidArgumentException
.
setting the maximum content rating
Gravatar provides four levels for rating avatars by, which are named similar to entertainment media ratings scales used in the United States. They are, by order of severity (first is safe for everyone to see, last is explicit), "g", "pg", "r", and "x".
By default, the maximum content rating is set to "g". You can set the maximum allowable rating on avatars embedded within your site by using the method emberlabsGravatarLibGravatar->setMaxRating()
. Please note that any avatars that do not fall under your maximum content rating will be replaced with the default image you have specified.
Here's an example of how to set the maximum content rating:
$gravatar->setMaxRating('r');
WARNING
If an invalid maximum rating is specified, this method will throw an exception of class InvalidArgumentException
.
enabling secure images
If your site is served over HTTPS, you'll likely want to serve gravatars over HTTPS as well to avoid "mixed content warnings".
To enable "secure images" mode, call the method emberlabsGravatarLibGravatar->enableSecureImages()
before generating any gravatar URLs.
To check to see if you are using "secure images" mode, call the method emberlabsGravatarLibGravatar->usingSecureImages()
, which will return a boolean value regarding whether or not secure images mode is enabled.
twig integration
It's extremely easy to hook this library up as a template asset to the Twig template engine.
When you've got an instance of the Twig_Environment ready, add in your instantiated gravatar object as a twig "global" like so:
<?php // include the lib file here, or use an autoloader if you wish include __DIR__ . '/includes/emberlabs/GravatarLib/Gravatar.php'; // instantiate the gravatar library object $gravatar = new emberlabsGravatarLibGravatar(); // ... do whatever you want with your settings here // here, we will assume $twig is an already-created instance of Twig_Environment $twig->addGlobal('gravatar', $gravatar);
Now in your twig templates, you can get a user's gravatar with something like this snip of code:
(note: this template snip assumes that the "email" template variable contains the email of the user to grab the gravatar for)
<img src="User avatar" src="{{ gravatar.get(email)|raw }}" />
We are also using the raw filter here to preserve XHTML 1.0 Strict compliance; the generated gravatar URL contains the &
character, and if the filter was not used it would be double-escaped.
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。