1. Go to this page and download the library: Download legitphp/hash-money 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/ */
$hash = DHash::hashFromFile('image.jpg', 64);
// Check individual bits
for ($i = 0; $i < 8; $i++) {
if ($hash->getBit($i)) {
echo "Bit $i is set\n";
}
}
// Count set bits (useful for hash analysis)
$setBits = $hash->countSetBits();
$density = $setBits / $hash->getBits(); // Bit density ratio
// Get normalized value (0.0 to 1.0)
$normalized = $hash->normalized();
$hash1 = PerceptualHash::hashFromFile('original.jpg');
$hash2 = PerceptualHash::hashFromFile('modified.jpg');
// Direct Hamming distance calculation
$distance = $hash1->hammingDistance($hash2);
// Calculate similarity percentage
$maxDistance = $hash1->getBits();
$similarity = (1 - ($distance / $maxDistance)) * 100;
echo "Images are {$similarity}% similar";
// Use normalized values for threshold comparisons
if ($hash1->normalized() > 0.5 && $hash2->normalized() > 0.5) {
echo "Both images have high bit density";
}
use LegitPHP\HashMoney\MultiIndexHash;
$hash = PerceptualHash::hashFromFile('image.jpg');
$chunks = MultiIndexHash::chunks($hash, 8); // 8 × 8-bit chunks, safe for k ≤ 7
// Persist chunks as BIGINT columns (mih_0..mih_7), each with its own index
// ... then query:
// SELECT * FROM hashes
// WHERE mih_0=? OR mih_1=? OR mih_2=? ... OR mih_7=?
// Union is your candidate set; verify with hammingDistance() in PHP.
// Use MashedHash + pHash for best results
$mHash = MashedHash::hashFromFile($image);
$pHash = PerceptualHash::hashFromFile($image);
if (MashedHash::distance($mHash1, $mHash2) < 20 &&
PerceptualHash::distance($pHash1, $pHash2) < 12) {
// High confidence match
}
// Use all three spatial/color hashes
$pHash = PerceptualHash::hashFromFile($image);
$dHash = DHash::hashFromFile($image);
$colorHash = ColorHistogramHash::hashFromFile($image);
// One 256-bit composite instead of four separate hashes — lets you
// band-index each chunk and do indexed candidate generation in the DB.
$composite = CompositeHash::default();
$hash = $composite->hashFromFile($image);
$bucketKeys = Lsh::bandsByChunk($hash, bandsPerChunk: 4);
use LegitPHP\HashMoney\PdqHash;
use LegitPHP\HashMoney\Strategies\PdqHashStrategy;
$hash = PdqHash::hashFromFile($image);
$quality = PdqHash::quality($hash);
// Meta's recommended thresholds
if ($quality < PdqHashStrategy::RECOMMENDED_QUALITY_THRESHOLD) {
// Image hashes unreliably (uniform / blurry) — skip or fall back to MashedHash.
return null;
}
if (PdqHash::distance($hash, $candidate) <= PdqHashStrategy::RECOMMENDED_DISTANCE_THRESHOLD) {
// Near-duplicate (≤ 31 of 256 bits differ).
}
// Catch rotated / flipped duplicates by hashing all eight dihedral variants once
// and matching the candidate's "orig" hash against any of them.
$variants = PdqHash::hashesFromFile($image);
foreach ($variants['hashes'] as $name => $variantHash) {
if (PdqHash::distance($variantHash, $candidate) <= 31) {
// Match under transform "$name" (orig / r090 / r180 / r270 / flpx / flpy / flpp / flpm).
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.