Skip to content

Novis\Image\Image::hash

Returns a short hash of an image, so you can find copies of the same picture.

Image::hash reads an encoded image, such as a PNG or JPEG file, and returns a few bytes. You can pass bytes, an open Core\IO\File or an uploaded Core\Request\Part. Two images that look the same give hashes that are almost equal, even when one is smaller or saved in another format. You compare two hashes with Image::hashDistance.

The Image::HashKind you pass sets how the hash is made and how long it is: 8 bytes for Perceptual, 16 bytes for Difference and 32 bytes for Average. Bytes that are not an image throw a ParseError.

Good to know: a hash is small, so you can save it in a database next to each image. You then compare a new upload with the saved hashes and never decode the old images again.

The examples below show the length of each kind, saving a hash as text, and the error for bytes that are not an image.

Parameters

  • $dataCore\Bytes::Source
  • $kindHashKind

Return value

Returns bytes.

Examples

One hash of each kind

<?nvs
// Shows the hash each `Image::HashKind` returns for the same image.
use Novis\Image\Image;
// A pattern of 4 by 4 grey pixels, made 64 by 64 pixels wide.
tainted bytes $photo = Image::fromRaw({
width: 4,
height: 4,
pixels: Core\Encoding::fromHex(
'202020ffe0e0e0ff606060ffa0a0a0ff'
. '909090ff303030ffd0d0d0ff505050ff'
. 'f0f0f0ff707070ff101010ffc0c0c0ff'
. '404040ffb0b0b0ff808080ff000000ff',
),
})->resized({ width: 64, height: 64 })->encode();
// This prints 8, 16 and 32 bytes.
array<Image::HashKind> $kinds = [Image::HashKind::Perceptual, Image::HashKind::Difference, Image::HashKind::Average];
foreach ($kinds as Image::HashKind $kind) {
bytes $hash = Image::hash($photo, $kind);
echo Core\Bytes::length($hash), ' bytes: ', Core\Encoding::toHex($hash), "\n";
}
Output
8 bytes: c6870fe4e51bf070
16 bytes: 100844225028140a0022110804824100
32 bytes: 0f0f0f0f0f0f0f0ff0f0f0f0f0f0f0f0f00ff00ff00ff00f0ff00ff00ff00ff0

Save the hash as text

<?nvs
// Shows how a blog saves the hash of each uploaded photo as text, and finds a copy later.
use Novis\Image\Format;
use Novis\Image\Image;
Image $photo = Image::fromRaw({
width: 4,
height: 4,
pixels: Core\Encoding::fromHex(
'202020ffe0e0e0ff606060ffa0a0a0ff'
. '909090ff303030ffd0d0d0ff505050ff'
. 'f0f0f0ff707070ff101010ffc0c0c0ff'
. '404040ffb0b0b0ff808080ff000000ff',
),
})->resized({ width: 64, height: 64 });
// `Core\Encoding::toHex` turns the hash into text you can save in a database column.
string $saved = Core\Encoding::toHex(Image::hash($photo->encode(), Image::HashKind::Perceptual));
echo 'saved: ', $saved, "\n";
// Later, the same photo is uploaded again as a JPEG file at half the size.
tainted bytes $upload = $photo->resized({ width: 32, height: 32 })->withFormat(Format::Jpeg)->encode();
bytes $hash = Image::hash($upload, Image::HashKind::Perceptual);
// The distance is small, so this prints "already on the blog".
uint $distance = Image::hashDistance(Core\Encoding::fromHex($saved), $hash);
echo $distance <= 10 ? 'already on the blog' : 'new photo', "\n";
Output
saved: c6870fe4e51bf070
already on the blog

Text is not an image

<?nvs
// Shows the error `Image::hash` throws for bytes that are not an image.
use Novis\Image\Image;
bytes $upload = 'This is a text file.' as bytes;
// The bytes are not an image, so this throws a `ParseError`.
try {
echo Core\Encoding::toHex(Image::hash($upload, Image::HashKind::Perceptual)), "\n";
} catch (ParseError $e) {
echo "ParseError: the upload is not an image\n";
}
Output
ParseError: the upload is not an image