Gravatar URL builder which is most commonly called as a Gravatar library

v2.0.0 2019-01-08 21:43 UTC

This package is auto-updated.

Last update: 2024-05-29 03:56:19 UTC


GitHub Workflow Status Total Downloads

Gravatar URL builder (aka. a Gravatar library)


Via Composer

$ composer require gravatarphp/gravatar


Create a Gravatar instance and use it for creating URLs.

use Gravatar\Gravatar;

// Defaults: no default parameter, use HTTPS
$gravatar = new Gravatar([], true);

// Returns

// Returns
// The fourth parameter enables validation and will prevent the
// size parameter from being added to the URL generated.
$gravatar->avatar('', ['s' => 9001], true, true);

// Returns

// Returns

// Returns

You can override the globally used protocol (HTTP, HTTPS) by setting the last parameter to true/false.

use Gravatar\Gravatar;

$gravatar = new Gravatar();

// Returns
$gravatar->avatar('', [], false);

// Returns
$gravatar->profile('', false);

// Returns
$gravatar->vcard('', false);

// Returns
$gravatar->qrCode('', false);

Last, but not least, you can pass default options to the builder and use them to generate avatar URLs.

use Gravatar\Gravatar;

$gravatar = new Gravatar([
    'size' => 500,

// Returns
$gravatar->avatar('', ['r' => 'g']);


If you pass any of the following parameters and turn validation on (fourth parameter in the avatar() method), their values will be checked against the allowed values defined in the Gravatar documentation:

  • s, size -- The image size
  • d, default -- The default image to display if there is no matching Gravatar
  • f, forcedefault -- Tell Gravatar to use the default image even if there is a matching Gravatar
  • r, rating -- The audience rating (G, R, etc.) to restrict the Gravatar to

If the value fails validation, an InvalidArgumentException will be thrown. Any parameters not listed above are not sanitized or validated in anyway.


Profile, vCard and QR Code requests will only work with the primary email address. This is a limitation of Gravatar. However the builder won't complain, since it doesn't know if it is your primary address or not. For more tips and details check the Gravatar documentation.


$ composer test


The project is licensed under the MIT License.