Skip to content

Novis\Image\Image::hashDistance

Novis\Image\Image::hashDistance(bytes $a, bytes $b): uintNovis\Image\Image::hashDistance(
    bytes $a,
    bytes $b,
): uint

Counts how many bits differ between two image hashes.

You get a hash from Image::hash. Two images that look the same give hashes with a small distance, even when one is smaller, larger or saved in another format. Two different images give a large distance. A distance of 0 means the hashes are equal.

Both hashes must be of the same Image::HashKind. Each kind has its own length, and two hashes of different lengths throw a LogicError.

Good to know: the largest distance is the number of bits in the hash: 64 for Perceptual, 128 for Difference and 256 for Average. Choose your limit for "the same image" from that number.

The examples below show the distance of two short hashes, a check for a duplicate upload, and the error for two kinds.

Parameters

  • $abytes
  • $bbytes

Return value

Returns uint.

Examples

Count the bits

<?nvs
// Shows how `Image::hashDistance` counts the bits that differ between two hashes.
use Novis\Image\Image;
// Two hashes of 2 bytes each. Only the last byte is different.
// 0x0f is 00001111 and 0x0c is 00001100, so two bits differ.
bytes $first = Core\Encoding::fromHex('a10f');
bytes $second = Core\Encoding::fromHex('a10c');
// This prints 0 and then 2.
echo Image::hashDistance($first, $first), "\n";
echo Image::hashDistance($first, $second), "\n";
Output
0
2

Find a duplicate upload

<?nvs
// Shows how a shop checks whether a new product photo is already in its catalogue.
use Novis\Image\Image;
// A pattern of 4 by 4 grey pixels, made 64 by 64 pixels wide, is the saved photo.
Image $photo = Image::fromRaw({
width: 4,
height: 4,
pixels: Core\Encoding::fromHex(
'202020ffe0e0e0ff606060ffa0a0a0ff'
. '909090ff303030ffd0d0d0ff505050ff'
. 'f0f0f0ff707070ff101010ffc0c0c0ff'
. '404040ffb0b0b0ff808080ff000000ff',
),
})->resized({ width: 64, height: 64 });
bytes $saved = Image::hash($photo->encode(), Image::HashKind::Perceptual);
// The first upload is the same photo at a smaller size. The second is a grey image.
array<tainted bytes> $uploads = [
$photo->resized({ width: 32, height: 32 })->encode(),
Image::create(64, 64)->grayscaled()->encode(),
];
// A distance of 10 or less out of 64 bits counts as the same photo.
// This prints "duplicate" and then "new photo".
foreach ($uploads as tainted bytes $upload) {
uint $distance = Image::hashDistance($saved, Image::hash($upload, Image::HashKind::Perceptual));
echo $distance <= 10 ? 'duplicate' : 'new photo', "\n";
}
Output
duplicate
new photo

Two kinds do not compare

<?nvs
// Shows the error `Image::hashDistance` throws for two hashes of different kinds.
use Novis\Image\Color;
use Novis\Image\Image;
tainted bytes $photo = Image::create(16, 16, Color::hex('#2563eb'))->encode();
// A `Perceptual` hash is 8 bytes and an `Average` hash is 32 bytes.
bytes $perceptual = Image::hash($photo, Image::HashKind::Perceptual);
bytes $average = Image::hash($photo, Image::HashKind::Average);
// The lengths are different, so this throws a `LogicError`.
try {
echo Image::hashDistance($perceptual, $average), "\n";
} catch (LogicError $e) {
echo 'LogicError: ', $e->message, "\n";
}
Output
LogicError: the hashes are 8 and 32 bytes long, and `hashDistance` needs two hashes of the same kind