Skip to content

API reference

This page is intentionally simple for now. Replace with generated stubs later if preferred.

ImageHashIndex

Main in-memory index.

php
$index = new ImageHashIndex();

add()

Add a precomputed hash.

php
$index->add(string $id, string $hash): void

Example:

php
$index->add('image-1', 'f123456789abcdef');

Search by hash.

php
$index->search(string $hash, int $maxDistance = 10, int $limit = 10): array

Example:

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

searchImage()

Search by image.

php
$matches = $index->searchImage('exmaple.png', maxDistance: 5, limit: 20);

nearest()

Find the nearest indexed match.

php
$index->nearest(string $hash): array

Example:

php
$index->add('near', 'aaaaaaaaaaaaaaab');
$index->add('far', 'ffffffffffffffff');
$nearest = $index->nearest('aaaaaaaaaaaaaaaa');

Output:

php
[
    'id' => 'near'
    'distance' => 2
]

nearestImage()

Find the closest matching image.

php
$index->nearest(string $path): array

Exmaple:

php
$index->addImage('cat', 'cat.jpg');
$index->addImage('dog', 'dog.jpg');
$match = $index->nearestImage('cat-cropped.jpg');

Output:

php
[
    'id' => 'cat',
    'distance' => 2
]

save()

Save the index to disk.

php
$index->save(string $path): void

load()

Load a saved index.

php
$index = ImageHashIndex::load(string $path);

Result shape

Expected result shape:

php
[
    [
        'id' => 'image-1',
        'distance' => 0,
    ],
    [
        'id' => 'image-2',
        'distance' => 2,
    ],
]

stats

Show curent index statistics

php
$index->stats()
php
$stats = $index->stats();
print_r($stats);

Example output

php
[
    'hashes' => 5000000
    'buckets' => 65536
    'memory_bytes' => 184320000
]
FieldDescription
hashesTotal hashes stored in the index
bucketsInternal bucket count used for candidate pruning
memory_bytesApproximate memory used by the index

Native tools, weird experiments, and practical performance work.