Skip to content

Perceptual image hashes (aHash, dHash and pHash) for GD and Imagick

Notifications You must be signed in to change notification settings

kodus/image-hash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kodus/image-hash

This library implements the aHash, dHash and pHash image-hashing algorithms as described in this article.

It's based on this implementation but with a loader-abstraction for GD and Imagick support, added tests, and a benchmark.

Usage

To compare two images:

use Kodus\ImageHash\ImageHasher;

$hasher = new ImageHasher();

$a_hash = $hasher->pHash("path/to/image-a.jpg");
$b_hash = $hasher->pHash("path/to/image-b.jpg");

if ($hasher->getDistance($a_hash, $b_hash) <= 2) {
    echo "same!";
} else {
    echo "different.";
}

Substitute calls to pHash() for aHash() or dHash() to use another algorithm, but note that getDistance() only makes sense for two hashes computed using the same algorithm.

Hashes are returned as a binary-mask string - use base_convert if you need a decimal or hex value.

Hacking

To run the tests:

php test/test.php

To run the benchmark:

php test/benchmark.php

If you hack on this library, be sure to run the benchmark before/after making changes.

About

Perceptual image hashes (aHash, dHash and pHash) for GD and Imagick

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages