{gravatar}

Writes the Gravatar picture of an e-mail address as an img - only a hash of the address leaves the page - or a local avatar instead.

Pictures and media Single tags

Syntax

{gravatar $email}
{gravatar $email, size=64, default='identicon', alt='text'}
{gravatar $email, fallback='avatar', alt='text'}

Parameters

email
The e-mail address - trimmed and lower-cased before it is hashed.

Options

size
Width and height in pixels, default 64; the srcset asks twice that for a sharp screen.
default
What Gravatar shows for an address it does not know: mp (default), identicon, monsterid, wavatar, retro, robohash, blank, 404, or an https:// image address.
alt
The image text - the person's name - default Avatar.
fallback
avatar draws the {avatar} initials of alt (or of the address) instead, identicon the {identicon} of the address - no <img>, no outside request.

Description

{gravatar} shows the picture a person registered for their e-mail address at gravatar.com (tags/_lib/gravatar.php). The address itself never leaves the page: the image address is https://www.gravatar.com/avatar/<sha256>?s=<size>&d=<default>, made from the SHA-256 hash of the trimmed, lower-cased address.

The <img class="pad-gravatar"> carries width and height, loading="lazy", decoding="async", referrerpolicy="no-referrer" and a srcset at twice the size. default chooses what Gravatar shows for an address it does not know.

Showing the picture still tells Gravatar the visitor's IP address. For a page whose privacy policy allows no outside requests - or a test without network - fallback draws a picture on the server instead and writes no <img> at all: fallback='avatar' the {avatar} initials of alt (or of the address), fallback='identicon' the {identicon} of the address.

A missing address, an unknown default or fallback is an error under the strict check.

Examples

Comments with Gravatars

An img per address, made from its SHA-256 hash; default=identicon for addresses Gravatar does not know.

Templateexamples/gravatar/1-comments.pad
<div style="display:grid; gap:12px">
  {comments}
  <div style="display:flex; gap:10px; align-items:flex-start">{gravatar $email, size=40, default='identicon', alt=$name} <div><strong>{$name}</strong><br>{$text}</div></div>
  {/comments}
</div>
PHPexamples/gravatar/1-comments.php
<?php

  $comments = [
    [ 'name' => 'Ada Lovelace', 'email' => 'ada@example.org',   'text' => 'Could the sequence tags take a step of their own?' ],
    [ 'name' => 'Grace Hopper', 'email' => 'grace@example.net', 'text' => 'A bug report with a template line number - lovely.' ] ];

?>
Resultopen alone
Ada Lovelace
Ada Lovelace
Could the sequence tags take a step of their own?
Grace Hopper
Grace Hopper
A bug report with a template line number - lovely.

No request to a third party

fallback= draws the picture on the server - the initials of alt=, or the identicon of the address.

Templateexamples/gravatar/2-fallback.pad
<p style="display:flex; gap:10px; align-items:center">
  {gravatar 'ada@example.org', size=40, fallback='avatar', alt='Ada Lovelace'}
  {gravatar 'ada@example.org', size=40, fallback='identicon', alt='Ada Lovelace'}
</p>
Resultopen alone

Ada Lovelace AL Ada Lovelace

See also

  • {avatar} Draws an initials avatar as inline SVG - the letters of a name in a circle coloured by a hash of it, no image needed.
  • {identicon} Draws a GitHub-style identicon as inline SVG - a symmetric 5 by 5 pattern and a colour from a hash of the value.
  • {img} Writes a picture of the application's www directory as a thumbnail of the size the page shows it at, resized once with GD.