Skip to content
On this page
Sponsor

Use the HTTP-API as Gravatar default image

You can use the HTTP API of DiceBear as Gravatar default image. But before that, let's take a look at the conditions for Gravatar default images:

  1. ✅ MUST be publicly available (e.g. cannot be on an intranet, on a local development machine, behind HTTP Auth or some other firewall etc). Default images are passed through a security scan to avoid malicious content.
  2. ✅ MUST be accessible via HTTP or HTTPS on the standard ports, 80 and 443, respectively.
  3. ⚠️ MUST have a recognizable image extension (jpg, jpeg, gif, png)
  4. ⚠️ MUST NOT include a querystring (if it does, it will be ignored)

Source: https://de.gravatar.com/site/implement/images/

Since Gravatar does not support SVG, we have to use the PNG endpoint.

js
const emailHash = encodeURIComponent('00000000000000000000000000000000');
const defaultImage = encodeURIComponent(
  'https://api.dicebear.com/5.x/lorelei/svg' 
  'https://api.dicebear.com/5.x/lorelei/png' 
);

const gravatarImage = `https://www.gravatar.com/avatar/${emailHash}?d=${defaultImage}`;
// https://www.gravatar.com/avatar/00000000000000000000000000000000?d=https%3A%2F%2Fapi.dicebear.com%2F5.x%2Florelei%2Fpng
php
$emailHash = urlencode('00000000000000000000000000000000');
$defaultImage = urlencode(
  'https://api.dicebear.com/5.x/lorelei/svg' 
  'https://api.dicebear.com/5.x/lorelei/png' 
);

$gravatarImage = sprintf(
  'https://www.gravatar.com/avatar/%s?d=%s',
  $emailHash,
  $defaultImage
);
// https://www.gravatar.com/avatar/00000000000000000000000000000000?d=https%3A%2F%2Fapi.dicebear.com%2F5.x%2Florelei%2Fpng

Usually we set options in the query string, such as the seed. Since a query string is not allowed by Gravatar, the HTTP-API allows you to specify the options in the path. Just replace the question mark with a slash.

js
const emailHash = encodeURIComponent('00000000000000000000000000000000');
const defaultImage = encodeURIComponent(
  `https://api.dicebear.com/5.x/lorelei/png?seed=${emailHash}` 
  `https://api.dicebear.com/5.x/lorelei/png/seed=${emailHash}` 
);

const gravatarImage = `https://www.gravatar.com/avatar/${emailHash}?d=${defaultImage}`;
// https://www.gravatar.com/avatar/00000000000000000000000000000000?d=https%3A%2F%2Fapi.dicebear.com%2F5.x%2Florelei%2Fpng%2Fseed%3D00000000000000000000000000000000
php
$emailHash = urlencode('00000000000000000000000000000000');
$defaultImage = urlencode(
  sprintf('https://api.dicebear.com/5.x/lorelei/svg?seed=%s', $emailHash) 
  sprintf('https://api.dicebear.com/5.x/lorelei/svg/seed=%s', $emailHash) 
);

$gravatarImage = sprintf(
  'https://www.gravatar.com/avatar/%s?d=%s',
  $emailHash,
  $defaultImage
);
// https://www.gravatar.com/avatar/00000000000000000000000000000000?d=https%3A%2F%2Fapi.dicebear.com%2F5.x%2Florelei%2Fpng