PHP code example of travail / image-perceptualhash

1. Go to this page and download the library: Download travail/image-perceptualhash library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

travail / image-perceptualhash example snippets




use Image\PerceptualHash;
use Image\PerceptualHash\Algorithm\DifferenceHash;
use Image\PerceptualHash\Algorithm\PerceptionHash;

// Create an instance of Image\PerceptualHash, at the same time calculate hashes
// using hashing algorithm Image\PerceptualHash\Algorithm\AverageHash by default
$ph = new PerceptualHash('/path/to/foo.jpg');

// Get binary hash
$binary_hash = $ph->bin();

// Get hexadecimal hash
$hex_hash = $ph->hex();

// Compare with another image, return a Hamming distance
$distance = $ph->compare('/path/to/bar.jpg');

// Calculate a similarity
$similarity = $ph->similarity('/path/to/baz.jpg');

// Calculate by other hashing algorithms
$ph = new PerceptualHash('/path/to/foo.jpg', new DifferenceHash());
// or
$ph = new PerceptualHash('/path/to/foo.jpg', new PerceptionHash());

Image\PerceptualHash __construct($file, Algorithm $algorithm)

string bin()

int compare(string|resource $file)

int distance(string $hash1, string $hash2)

double similarity(string|resource $file)