Skip to content

Quick start

Doc's are boring, start coding

Create an index

php
$index = new ImageHashIndex();

The index is where hashes are stored.

Add images by hash

php
$index->add('hex', 'f123456789abcdef');
$index->add('myImage', 'example.png');
$index->add('password', 'horse staple battery');

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
// By Hash
$matches = $index->search(
    'f123456789abcdef',
    maxDistance: 5,
    limit: 20
);
// By Image
$matches = $index->searchImage(
    'holiday-pic.png',
    maxDistance: 5,
    limit: 5
);

That means:

text
Find images that look close to this hash.
Only return results with distance 5 or below.
Return at most 20 matches.

Find nearest match

php
$index->addImage('cat', 'cat.jpg');
$index->addImage('dog', 'dog.jpg');
$match = $index->nearestImage('cat-cropped.jpg');
/**
 * Array
 *(
 *  [0] => Array
 *    (
 *      [id] => cat
 *      [distance] => 0
 *    )
 *)
 * 
 **/
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.