PHP implementation search for similar images, _php tutorial

Source: Internet
Author: User

PHP implementation to search for similar images,


Perceptual hashing algorithm

Count < = 5 Match most similar
Count > 102 Different pictures
Var_dump (Imagehash::run ('./1.png ', './psb.jpg '));

<?phpclass 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; }}var_dump (Imagehash::run ('./1.png ', './psb.jpg '));

Method Two:

hash ($f); } return $isString? $result [0]: $result; The Public Function checkissimilarimg ($imgHash, $otherImgHash) {if (file_exists ($imgHash) && file_exists ($  Otherimghash) {$imgHash = $this->run ($imgHash); $otherImgHash = $this->run ($otherImgHash); } if (strlen ($imgHash)!== strlen ($otherImgHash)) return false; $count = 0; $len = strlen ($imgHash);  for ($i =0; $i < $len; $i + +) {if ($imgHash {$i}!== $otherImgHash {$i}) {$count + +; }} return $count <= (5 * $rate * $rate)? True:false; The Public Function hash ($file) {if (!file_exists ($file)) {return false;} $height = 8 * $this->rate; $width = 8 * $this ->rate; $img = Imagecreatetruecolor ($width, $height); List ($w, $h) = getimagesize ($file); $source = $this->createimg ($file); Imagecopyresampled ($img, $source, 0, 0, 0, 0, $width, $height, $w, $h); $value = $this->gethashvalue ($img); Imagedestroy ($IMG); return $value; The Public Function Gethashvalue ($img) {$width = Imagesx ($img); $height = Imagesy ($img); $total= 0; $array = Array (); for ($y =0; $y < $height; $y + +) {for ($x =0; $x < $width; $x + +) {$gray = (Imagecolorat ($img, $x, $y) >> 8) & 0xF  F  if (!is_array ($array [$y])) {$array [$y] = array ();  } $array [$y] [$x] = $gray;  $total + = $gray; }} $average = Intval ($total/(* $this->rate * $this->rate)); $result = ";  for ($y =0; $y < $height; $y + +) {for ($x =0; $x < $width; $x + +) {if ($array [$y] [$x] >= $average) {$result. = ' 1 ';  }else{$result. = ' 0 '; }}} return $result; Public Function createimg ($file) {$ext = $this->getfileext ($file), if ($ext = = = ' jpeg ') $ext = ' jpg '; $img = null; SW  Itch ($ext) {case ' png ': $img = Imagecreatefrompng ($file);  Case ' jpg ': $img = Imagecreatefromjpeg ($file); Case ' gif ': $img = Imagecreatefromgif ($file); } return $img; The Public Function Getfileext ($file) {$infos = explode ('. ', $file); $ext = Strtolower ($infos [Count ($infos)-1]); return $e xt }}

The method is called as follows:

Require_once "Imghash.class.php"; $instance = Imghash::getinstance (); $result = $instance->checkissimilarimg (' Chenyin/img_3214.png ', ' chenyin/img_3212.jpg ');

If the $result value is true, it indicates that 2 pictures are similar, otherwise not similar.

http://www.bkjia.com/PHPjc/1057474.html www.bkjia.com true http://www.bkjia.com/PHPjc/1057474.html techarticle PHP Implementation search similar images, perceptual hashing algorithm count = 5 matches most similar count 102 different picture var_dump (Imagehash::run ('./1.png ', './psb.jpg ')); Phpclass Imageh Ash {...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.