PHP with Image Search

Source: Internet
Author: User

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

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.