Skip Navigation
Docs

Average Color

$file->blurhashColor()

Encodes the image with BlurHash and returns the average color as a hex string.

In order to still provide a small placeholder before a client-side library gets loaded, BlurHash also encodes the average color of an image which we can access and use to enhance lazy-loading when no scripts have been loaded yet.

We can update the snippet from the client-side decoding section like this:

<div
  data-blurhash="<?= $image->blurhash() ?>"
  style="aspect-ratio: <?= $image->ratio() ?>; background-color: <?= $image->blurhashColor() ?>;">
</div>

When you want to get the average color, but already have a BlurHash generated, you can use the averageColor static method on the BlurHash class instead:

<?php

use tobimori\BlurHash;

$blurhash = 'LKN]Rv%2Tw=w]~RBVZRi};RPxuwH';
echo BlurHash::averageColor($blurhash); // #d0b1a3