thomaswelton/gravatarlib
Composer 安装命令:
composer require thomaswelton/gravatarlib
包简介
A lightweight PHP 5.3 OOP library providing easy gravatar integration.
关键字:
README 文档
README
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 algorithm
usage
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 \emberlabs\GravatarLib\Gravatar(); // 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 \emberlabs\GravatarLib\Gravatar->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 \emberlabs\GravatarLib\Gravatar->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 \emberlabs\GravatarLib\Gravatar->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 \emberlabs\GravatarLib\Gravatar->enableSecureImages() before generating any gravatar URLs.
To check to see if you are using "secure images" mode, call the method \emberlabs\GravatarLib\Gravatar->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 \emberlabs\GravatarLib\Gravatar(); // ... 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.
thomaswelton/gravatarlib 适用场景与选型建议
thomaswelton/gravatarlib 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.13M 次下载、GitHub Stars 达 25, 最近一次更新时间为 2014 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「templating」 「gravatar」 「twig」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 thomaswelton/gravatarlib 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thomaswelton/gravatarlib 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 thomaswelton/gravatarlib 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A library providing easy gravatar integration/generation (Laravel supported).
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Library to validate and parse social media profile URLs
Caching and compression for Twig assets (JavaScript and CSS).
统计信息
- 总下载量: 1.13M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 18
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-03-02