2017 © Pedro Peláez
 

library imagehash

Perceptual image hashing for PHP

image

jenssegers/imagehash

Perceptual image hashing for PHP

  • Thursday, July 26, 2018
  • by jenssegers
  • Repository
  • 66 Watchers
  • 1134 Stars
  • 129,137 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 100 Forks
  • 10 Open issues
  • 14 Versions
  • 15 % Grown

The README.md

ImageHash

Latest Stable Version Build Status Coverage Status Donate, (*1)

A perceptual hash is a fingerprint of a multimedia file derived from various features from its content. Unlike cryptographic hash functions which rely on the avalanche effect of small changes in input leading to drastic changes in the output, perceptual hashes are "close" to one another if the features are similar., (*2)

, (*3)

Perceptual hashes are a different concept compared to cryptographic hash functions like MD5 and SHA1. With cryptographic hashes, the hash values are random. The data used to generate the hash acts like a random seed, so the same data will generate the same result, but different data will create different results. Comparing two SHA1 hash values really only tells you two things. If the hashes are different, then the data is different. And if the hashes are the same, then the data is likely the same. In contrast, perceptual hashes can be compared -- giving you a sense of similarity between the two data sets., (*4)

This code was inspired/based on: - https://github.com/kennethrapp/phasher - http://www.phash.org - http://blockhash.io - http://www.hackerfactor.com/blog/?/archives/529-Kind-of-Like-That.html - http://www.hackerfactor.com/blog/?/archives/432-Looks-Like-It.html - http://blog.iconfinder.com/detecting-duplicate-images-using-python, (*5)

Requirements

  • PHP 8.1 or higher
  • The gd or imagick extension
  • Optionally, install the GMP extension for faster fingerprint comparisons

Installation

This package has not reached a stable version yet, backwards compatibility may be broken between 0.x releases. Make sure to lock your version if you intend to use this in production!, (*6)

Install using composer:, (*7)

composer require jenssegers/imagehash

Usage

The library comes with 4 built-in hashing implementations:, (*8)

  • Jenssegers\ImageHash\Implementations\AverageHash - Hash based the average image color
  • Jenssegers\ImageHash\Implementations\DifferenceHash - Hash based on the previous pixel
  • Jenssegers\ImageHash\Implementations\BlockHash - Hash based on blockhash.io Still under development
  • Jenssegers\ImageHash\Implementations\PerceptualHash - The original pHash Still under development

Choose one of these implementations. If you don't know which one to use, try the DifferenceHash implementation. Some implementations allow some configuration, be sure to check the constructor., (*9)

use Jenssegers\ImageHash\ImageHash;
use Jenssegers\ImageHash\Implementations\DifferenceHash;

$hasher = new ImageHash(new DifferenceHash());
$hash = $hasher->hash('path/to/image.jpg');

echo $hash;
// or
echo $hash->toHex();

The resulting Hash object, is a hexadecimal image fingerprint that can be stored in your database once calculated. The hamming distance is used to compare two image fingerprints for similarities. Low distance values will indicate that the images are similar or the same, high distance values indicate that the images are different. Use the following method to detect if images are similar or not:, (*10)

$distance = $hasher->distance($hash1, $hash2);
// or
$distance = $hash1->distance($hash2);

Equal images will not always have a distance of 0, so you will need to decide at which distance you will evaluate images as equal. For the image set that I tested, a max distance of 5 was acceptable. But this will depend on the implementation, the images and the number of images. For example; when comparing a small set of images, a lower maximum distances should be acceptable as the chances of false positives are quite low. If however you are comparing a large amount of images, 5 might already be too much., (*11)

The Hash object can return the internal binary hash in a couple of different format:, (*12)

echo $hash->toHex(); // 7878787c7c707c3c
echo $hash->toBits(); // 0111100001111000011110000111110001111100011100000111110000111100
echo $hash->toInt(); // 8680820757815655484
echo $hash->toBytes(); // "\x0F\x07ƒƒ\x03\x0F\x07\x00"

Choose your preference for storing your hashes in your database. If you want to reconstruct a Hash object from a previous calculated value, use:, (*13)

$hash = Hash::fromHex('7878787c7c707c3c');
$hash = Hash::fromBin('0111100001111000011110000111110001111100011100000111110000111100');
$hash = Hash::fromInt('8680820757815655484');

Demo

These images are similar:, (*14)

Equals1 Equals2, (*15)

Image 1 hash: 3c3e0e1a3a1e1e1e (0011110000111110000011100001101000111010000111100001111000011110)
Image 2 hash: 3c3e0e3e3e1e1e1e (0011110000111110000011100011111000111110000111100001111000011110)
Hamming distance: 3

These images are different:, (*16)

Equals1 Equals2, (*17)

Image 1 hash: 69684858535b7575 (0010100010101000101010001010100010101011001010110101011100110111)
Image 2 hash: e1e1e2a7bbaf6faf (0111000011110000111100101101001101011011011101010011010101001111)
Hamming distance: 32

Security contact information

To report a security vulnerability, follow these steps., (*18)

The Versions

26/07 2018

dev-master

9999999-dev https://github.com/jenssegers/imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

05/05 2018

v0.5.0

0.5.0.0 https://github.com/jenssegers/imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

21/04 2017

v0.4.2

0.4.2.0 https://github.com/jenssegers/imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-gd *

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

21/02 2017

dev-analysis-8Ael0G

dev-analysis-8Ael0G https://github.com/jenssegers/imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-gd *

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

23/10 2016

v0.4.1

0.4.1.0 https://github.com/jenssegers/imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-gd *

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

20/02 2016

v0.4.0

0.4.0.0 https://github.com/jenssegers/imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

15/11 2015

v0.3.0

0.3.0.0 https://github.com/jenssegers/imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

15/11 2015

dev-develop

dev-develop https://github.com/jenssegers/imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

21/03 2015

v0.2.3

0.2.3.0 https://github.com/jenssegers/php-imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

16/03 2015

v0.2.2

0.2.2.0 https://github.com/jenssegers/php-imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

05/03 2015

v0.2.1

0.2.1.0 https://github.com/jenssegers/php-imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

02/03 2015

v0.2.0

0.2.0.0 https://github.com/jenssegers/php-imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

06/12 2014

v0.1.1

0.1.1.0 https://github.com/jenssegers/php-imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash

05/12 2014

v0.1.0

0.1.0.0 https://github.com/jenssegers/php-imagehash

Perceptual image hashing for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Kenneth Rapp

hash phash imagehash perceptual dhash ahash image hash