PHP library for gravatar.com

1.1.0 2015-02-01 16:27 UTC

This package is auto-updated.

Last update: 2024-11-28 07:05:27 UTC


README

Build Status SensioLabs Insight Coverage Status Scrutinizer Code Quality Total Downloads

PHP library for gravatar.com based on Guzzle 5.

API: emanueleminotto.github.io/Gravatar

Install

Install Silex using Composer.

Install the Gravatar library adding emanueleminotto/gravatar to your composer.json or from CLI:

$ composer require emanueleminotto/gravatar

Usage

This library exposes 5 APIs:

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 Extension

In this library there's included a Twig extension to allow a simple integration with frameworks.

{% 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 %}