Perceptual hashing algorithm
Count < = 5 Match most similar
Count > 102 Different pictures
Var_dump (Imagehash::run (' 1.jpg ', ' 2.jpg '));
<?
Class Imagehash {Const File_not_found = '-1 '; Const File_extname_illegal = '-2 '; Private Function __construct () {} public static function run ($src 1, $src 2) {static $self; if (! $self) $self = new Static; if (!is_file ($src 1) | |!is_file ($SRC 2)) exit (Self::file_not_found); $hash 1 = $self->gethashvalue ($src 1); $hash 2 = $self->gethashvalue ($src 2); if (strlen ($hash 1)!== strlen ($hash 2)) return false; $count = 0; $len = strlen ($hash 1); for ($i = 0; $i < $len; $i + +) if ($hash 1[$i]!== $hash 2[$i]) $count + +; Return $count <= 10? True:false; Public Function GetImage ($file) {$extname = PathInfo ($file, pathinfo_extension); if (!in_array ($extname, [' jpg ', ' jpeg ', ' png ', ' gif ')]) exit (Self::file_extname_illegal); $img = Call_user_func (' Imagecreatefrom '. ($extname = = ' jpg '? ' JPEG ': $extname), $file); Return $img; The Public Function Gethashvalue ($file) {$w = 8; $h = 8; $img = Imagecreatetruecolor ($w, $h); List ($src _w, $src _h) = getimagesize ($file); $SRC = $this->getimage ($file); Imagecopyresampled ($img, $src, 0, 0, 0, 0, $w, $h, $src _w, $src _h); Imagedestroy ($SRC); $total = 0; $array = Array (); for ($y = 0; $y < $h; $y + +) {for ($x = 0; $x < $w; $x + +) {$gray = (Imagecolorat ($img, $x , $y) >> 8) & 0xFF; if (!isset ($array [$y])) $array [$y] = array (); $array [$y] [$x] = $gray; $total + = $gray; }} Imagedestroy ($IMG); $average = Intval ($total/($W * $h * 2)); $hash = "; for ($y = 0; $y < $h; $y + +) {for ($x = 0; $x < $w; $x + +) {$hash. = ($array [$y] [$x] >= $ Average)? ' 1 ': ' 0 '; } } Var_dump ($hash); return $hash; } }
Call method Comparison returns the result bool value
Var_dump (Imagehash::run (' 1.jpg ', ' 2.jpg '));
PHP with Image Search