Skip to content

Quick start

This is the smallest useful example.

Create an index

php
$index = new ImageHashIndex();

The index is where hashes are stored.

Add images by hash

php
$index->add('image-1', 'f123456789abcdef');
$index->add('image-2', 'f123456789abcdee');
$index->add('image-3', '0011aa99bb44cc22');

The first argument is your ID. It can be a filename, database ID, URL, UUID or anything useful to your app.

The second argument is the perceptual hash.

php
$matches = $index->search(
    'f123456789abcdef',
    maxDistance: 5,
    limit: 20
);

That means:

text
Find images that look close to this hash.
Only return results with distance 5 or below.
Return at most 20 matches.
php
foreach ($matches as $match) {
    echo $match['id'] . ' distance=' . $match['distance'] . PHP_EOL;
}

Save and load

php
$index->save('/tmp/imagehash.idx');

$loaded = ImageHashIndex::load('/tmp/imagehash.idx');

TIP

Use save/load when building a large index once and reusing it across scripts or jobs.

Native tools, weird experiments, and practical performance work.