In This Article


The Gravatar helper is useful for rendering image HTML markup returned from the service.

Basic Usage

You can use the Gravatar helper anywhere in view scripts per the following example:

echo $this->gravatar('')->getImgTag();

The first (and only, in this example) argument passed to the Gravatar helper is an e-mail for which you want grab an avatar from For convenience, this e-mail will be automatically hashed via the md5 algorithm.

This will render the HTML below:

<img src="">

The helper already provides URL defaults for you.

Custom Settings

You can customize the request for a image by using setter methods on the view helper:

$gravatar = $this->gravatar();

// Set the email instead of passing it via helper invocation

// Set the image size you want to return, in pixels

// Set the default avatar image to use if does not find a match
$gravatar->setDefaultImg( \Zend\View\Helper\Gravatar::DEFAULT_MM );

// Set the avatar "rating" threshold (often used to omit NSFW avatars)
$gravatar->setRating( \Zend\View\Helper\Gravatar::RATING_G );

// Indicate that a secure URI should be used for the image source

// Render the <img> tag with the email you've set previously
echo $gravatar->getImgTag();

Alternately, you can pass an array as the second argument on invocation, with the following keys:

$settings = [
    'img_size'    => 40,
    'default_img' => \Zend\View\Helper\Gravatar::DEFAULT_MM,
    'rating'      => \Zend\View\Helper\Gravatar::RATING_G,
    'secure'      => null,
$email = '';
echo $this->gravatar($email, $settings);

Scheme autodiscovery

Passing null for the secure setting will cause the view helper to choose a schema that matches the current request to your application. This is the default behavior.

As you can see in the above examples, there are predefined settings for the default image and rating.

The Gravatar helper defines the following constants for ratings:


The helper defines the following constants for the default image:

  • DEFAULT_404

You may also provide custom attributes for the generated img tag. To do this, pass an attributes array to the setAttributes() method:

$gravatar = $this->gravatar('');

// Suppose that I want to add the class attribute with a value of
// "gravatarcls" to the rendered <img> tag:
$attr = [
    'class' => 'gravatarcls',
echo $gravatar->setAttributes($attr)->getImgTag();

Alternately, you can pass this array as the third argument during helper invocation:

$email = '';
$settings = [
    'default_img' => \Zend\View\Helper\Gravatar::DEFAULT_MM,
$attr = [
    'class' => 'gravatar-image',
    'id'    => 'gravatar',

echo $this->gravatar($email, $settings, $attr);

Found a mistake or want to contribute to the documentation? Edit this page on GitHub!