PHPackages                             kyliancodes/gravatar-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. kyliancodes/gravatar-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

kyliancodes/gravatar-bundle
===========================

Symfony bundle to integrate Gravatar service into your application

v1.0.0(1mo ago)0476↓50%MITPHPPHP &gt;=8.1CI passing

Since Apr 24Pushed 1mo agoCompare

[ Source](https://github.com/KylianCodes/GravatarBundle)[ Packagist](https://packagist.org/packages/kyliancodes/gravatar-bundle)[ RSS](/packages/kyliancodes-gravatar-bundle/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (13)Versions (5)Used By (0)

GravatarBundle
==============

[](#gravatarbundle)

A Symfony bundle to integrate [Gravatar](https://gravatar.com) into your application.
Supports avatar URLs, base64 privacy mode, profile data, form validation, CLI tooling, and Symfony Profiler integration.

[![Latest Version](https://camo.githubusercontent.com/95b7a79f13998f92d25755b12d6f56b6844afbf82bf4d8c151f0efa9ac490df0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b796c69616e636f6465732f67726176617461722d62756e646c652e737667)](https://packagist.org/packages/kyliancodes/gravatar-bundle)[![Total Downloads](https://camo.githubusercontent.com/bafee64b859e1cff67765c10465f9e807818e7acc80ffa39a701abc93976a875/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b796c69616e636f6465732f67726176617461722d62756e646c652e737667)](https://packagist.org/packages/kyliancodes/gravatar-bundle)[![License](https://camo.githubusercontent.com/9f03ec33752fd3721b7ec224fe7f7c0755ff39a04c6f2812e36a2d2196d453f9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b796c69616e636f6465732f67726176617461722d62756e646c652e737667)](LICENSE)

---

Quick Start
-----------

[](#quick-start)

```
composer require kyliancodes/gravatar-bundle
```

Create `config/packages/gravatar.yaml`:

```
gravatar:
    size: 80
    default: mp
```

Use in Twig:

```

{{ gravatar_tag('user@example.com', {size: 120, alt: 'Avatar', class: 'rounded'}) }}
```

That's it. See below for all available options and features.

---

Requirements
------------

[](#requirements)

- PHP **8.1** or higher
- Symfony **5.4**, **6.x**, **7.x**, or **8.x**

---

Installation
------------

[](#installation)

```
composer require kyliancodes/gravatar-bundle
```

If you are not using Symfony Flex, register the bundle manually in `config/bundles.php`:

```
return [
    KylianCodes\GravatarBundle\GravatarBundle::class => ['all' => true],
];
```

> **Note:** No Flex recipe is provided. You must create `config/packages/gravatar.yaml` manually (see Configuration below).

---

Configuration
-------------

[](#configuration)

Create `config/packages/gravatar.yaml` with your desired defaults:

```
gravatar:
    api_key: null         # Gravatar API key (recommended for profile data)
    size: 80              # Avatar size in pixels (1–2048). Default: 80
    rating: g             # Maximum content rating: g | pg | r | x. Default: g
    default: mp           # Fallback image: mp | identicon | monsterid | wavatar | retro | robohash | blank | 404
    format: url           # Output format: url | base64. Default: url
    cache:
        enabled: false    # Enable Symfony Cache for avatars and profiles. Default: false
        ttl: 3600         # Cache lifetime in seconds. Default: 3600
        pool: cache.app   # Symfony cache pool service ID. Default: cache.app
```

> All options are optional. The bundle works out of the box with no configuration.

---

Usage
-----

[](#usage)

### PHP Service

[](#php-service)

Inject `GravatarService` into any controller, service, or command:

```
use KylianCodes\GravatarBundle\Service\GravatarService;

class MyController extends AbstractController
{
    public function __construct(private GravatarService $gravatar) {}

    public function profile(): Response
    {
        // Avatar URL
        $url = $this->gravatar->getUrl('user@example.com');
        $url = $this->gravatar->getUrl('user@example.com', size: 120, rating: 'pg');

        // Avatar as base64 data URI (privacy mode — hash never exposed to browser)
        $img = $this->gravatar->getBase64('user@example.com');

        // Auto-detect format from configuration
        $src = $this->gravatar->get('user@example.com', size: 100);

        // Check if an email has a Gravatar account
        $exists = $this->gravatar->exists('user@example.com'); // bool

        // Fetch profile data (requires API key for full details)
        $profile = $this->gravatar->getProfile('user@example.com');
        // [
        //     'display_name' => 'John Doe',
        //     'description'  => 'PHP Developer',
        //     'links'        => [['label' => 'Blog', 'url' => 'https://...']],
        // ]

        // Get the avatar URL of the currently authenticated user
        $email = $this->gravatar->getCurrentUserEmail();
        $src   = $this->gravatar->get($email);
    }
}
```

---

### Twig

[](#twig)

Five functions are available in any Twig template.

#### `gravatar(email, size, rating, default, format)`

[](#gravataremail-size-rating-default-format)

Returns the avatar URL or base64 data URI.

```

```

---

#### `gravatar_tag(email, options)`

[](#gravatar_tagemail-options)

Returns a complete `` tag with `loading="lazy"` and XSS-safe attributes.

```
{{ gravatar_tag('user@example.com', {
    size: 80,
    alt: 'User avatar',
    class: 'avatar rounded',
    id: 'user-pic',
}) }}
```

Output:

```

```

---

#### `gravatar_exists(email)`

[](#gravatar_existsemail)

Returns `true` if the email has a Gravatar account.

```
{% if gravatar_exists('user@example.com') %}

{% else %}

{% endif %}
```

---

#### `gravatar_profile(email)`

[](#gravatar_profileemail)

Returns profile data as an array, or `null` if no profile exists.

```
{% set profile = gravatar_profile('user@example.com') %}

{% if profile %}
    {{ profile.display_name }}
    {{ profile.description }}

    {% for link in profile.links %}
        {{ link.label }}
    {% endfor %}
{% endif %}
```

---

#### `gravatar_user(size, rating, default, format)`

[](#gravatar_usersize-rating-default-format)

Returns the avatar for the currently authenticated Symfony user. Returns `null` if no user is logged in.

```
{% set avatar = gravatar_user() %}

{% if avatar %}

{% endif %}

{# Or with a custom size #}
{% set avatar = gravatar_user(40) %}
{% if avatar %}

{% endif %}
```

---

### CLI Command

[](#cli-command)

Check whether an email address has a Gravatar account from the terminal:

```
php bin/console gravatar:check user@example.com
```

**Output with a Gravatar account:**

```
 Gravatar Check
================

 Email: user@example.com

 [OK] Gravatar account found!

 URL: https://www.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=80&r=g&d=mp

 Profile
--------
 Name:  John Doe
 Bio:   PHP Developer
 Links:
   - Blog: https://example.com

```

**Output without a Gravatar account:**

```
 [WARNING] No Gravatar account found for this email.

```

---

### Form Validation

[](#form-validation)

Use `#[GravatarExists]` on any email property to validate that it has an associated Gravatar account:

```
use KylianCodes\GravatarBundle\Validator\GravatarExists;
use Symfony\Component\Validator\Constraints as Assert;

class ProfileType
{
    #[Assert\NotBlank]
    #[Assert\Email]
    #[GravatarExists]
    public string $email = '';
}
```

The constraint skips `null` and empty string values — combine it with `#[Assert\NotBlank]` as needed.

---

Privacy Mode
------------

[](#privacy-mode)

By default (`format: url`), the avatar URL containing the MD5 hash of the email is rendered directly in the HTML:

```

```

Setting `format: base64` fetches the image server-side and inlines it as a data URI. The hash never reaches the browser:

```

```

Enable globally:

```
gravatar:
    format: base64
    cache:
        enabled: true   # Strongly recommended with base64 to avoid fetching on every request
        ttl: 3600
```

Or per call:

```
{{ gravatar('user@example.com', format='base64') }}
```

---

Cache
-----

[](#cache)

Enable caching to avoid repeated HTTP requests to Gravatar (especially useful with `base64` format or frequent profile lookups):

```
gravatar:
    cache:
        enabled: true
        ttl: 3600         # seconds
        pool: cache.app   # any Symfony cache pool
```

Example with a dedicated Redis pool:

```
# config/packages/cache.yaml
framework:
    cache:
        pools:
            cache.gravatar:
                adapter: cache.adapter.redis

# config/packages/gravatar.yaml
gravatar:
    cache:
        enabled: true
        ttl: 7200
        pool: cache.gravatar
```

**What is cached:** `getBase64()` and `getProfile()` responses.
**What is not cached:** `getUrl()` (no HTTP call) and `exists()` (HEAD request, fast by design).

---

Symfony Profiler
----------------

[](#symfony-profiler)

When `kernel.debug` is `true`, a **Gravatar** panel appears in the Symfony Web Profiler toolbar showing:

- Total number of Gravatar calls per request
- Cache hit / miss per call
- Duration of each call in milliseconds
- Error status per call

No overhead in production — the collector is only wired in debug mode.

---

Content Security Policy (CSP)
-----------------------------

[](#content-security-policy-csp)

The required CSP directive depends on the configured `format`.

### `format: url` (default)

[](#format-url-default)

The browser fetches the avatar image directly from Gravatar. Allow the Gravatar CDN in your `img-src`:

```
Content-Security-Policy: img-src 'self' https://www.gravatar.com https://secure.gravatar.com https://*.gravatar.com;

```

### `format: base64` (privacy mode)

[](#format-base64-privacy-mode)

The image is fetched server-side and inlined as a `data:` URI. The browser makes no external request — no Gravatar domain needed:

```
Content-Security-Policy: img-src 'self' data:;

```

---

Contributing
------------

[](#contributing)

Contributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) before submitting a pull request.

Found a bug? [Open an issue](https://github.com/KylianCodes/GravatarBundle/issues/new/choose) — bug report and feature request templates are available.

---

Credits
-------

[](#credits)

This bundle was created and is maintained by **[Kylian](https://github.com/kyliancodes)**.

It was inspired by [Pyrrah/GravatarBundle](https://github.com/Pyrrah/GravatarBundle). The original project could not be used directly in a Symfony 8 application, so this bundle was created to modernize the idea with full compatibility for Symfony 5.4 through 8.x, PHP 8.1+, and the Gravatar REST API v3.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance91

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

44d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/bbc47d869460bd64a3fa7a8922e1cdf08ca222ca887e072e32e3202c8f0d115d?d=identicon)[kyliancodes](/maintainers/kyliancodes)

---

Top Contributors

[![KylianCodes](https://avatars.githubusercontent.com/u/274022549?v=4)](https://github.com/KylianCodes "KylianCodes (8 commits)")

---

Tags

phpsymfony-bundlesymfony5symfony6symfony7symfony8symfonybundlegravataravatar

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kyliancodes-gravatar-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/kyliancodes-gravatar-bundle/health.svg)](https://phpackages.com/packages/kyliancodes-gravatar-bundle)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
