用于 gravatar.com 的 PHP 库

1.1.0 2015-02-01 16:27 UTC

This package is auto-updated.

Last update: 2024-08-28 06:19:25 UTC


README

Build Status SensioLabs Insight Coverage Status Scrutinizer Code Quality Total Downloads

基于 gravatar.com 的 PHP 库,使用 Guzzle 5 构建。

API: emanueleminotto.github.io/Gravatar

安装

使用 Composer 安装 Silex。

通过在 composer.json 中添加 emanueleminotto/gravatar 或从 CLI 安装来安装 Gravatar 库

$ composer require emanueleminotto/gravatar

使用方法

此库公开了 5 个 API

use EmanueleMinotto\Gravatar\Client;

$client = new Client(/* optional Guzzle HTTP client */);

// user profile
$url = $client->getProfileUrl('user@example.com'); // https://www.gravatar.com/b58996c504c5638798eb6b511e6f49af.json
$qrcode = $client->getProfileUrl('user@example.com', 'qr'); // https://www.gravatar.com/b58996c504c5638798eb6b511e6f49af.qr
$qrcode = $client->getProfileUrl('user@example.com', 'json', [
    'callback' => 'alert',
]); // https://www.gravatar.com/b58996c504c5638798eb6b511e6f49af.json?callback=alert

$profile = $client->getProfile('user@example.com');
// array(
//   "id" => "b58996c504c5638798eb6b511e6f49af",
//   "hash" => "b58996c504c5638798eb6b511e6f49af",
//   "preferredUsername" => "example user",
//   ...
// )

// user avatar
$img = $client->getAvatarUrl('user@example.com'); // https://www.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af.jpg?d=404&r=g&s=80
$img = $client->getAvatarUrl('user@example.com', 150); // https://www.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af.jpg?d=404&r=g&s=150

$img = $client->getAvatar('user@example.com'); // data URI
$img = $client->getAvatar('user@example.com', 150); // data URI

$exists = $client->exists('user@example.com'); // true
$exists = $client->exists('wrong'); // false

Twig 扩展

在此库中包含了一个 Twig 扩展,允许与框架简单集成。

{% if email is gravatar %} {# this test check if the gravatar exists #}
    <a href="{{ email|gravatar_profile_url }}">
        <img
            src="{{ email|gravatar_url }}"
            alt="{{ gravatar_profile(email).profileUrl }}"
        />
    </a>
{% endif %}