/**
- * Image similarity comparison
- *
- * @ Version $ Id: ImageHash. php 4429 2012-04-17 13: 20: 31Z jajax $
- * @ Authorjax. hu
- * Www.osxue.com
- * // Sample_1
- * $ AHash = ImageHash: hashImageFile('wsz.11.jpg ');
- * $ BHash = ImageHash: hashImageFile('wsz.12.jpg ');
- * Var_dump (ImageHash: isHashSimilar ($ aHash, $ bHash ));
- *
- * // Sample_2
- * Var_dump (ImageHash: isImageFileSimilar('wsz.11.jpg ', 'wsz.12.jpg '));
- *
- */
Class ImageHash {
- /** Sampling rate: 1 ~ 10
- * @ Access public
- * @ Staticvar int
- **/
- Public static $ rate = 2;
/** Allowed similarity values: 0 ~ 64
- * @ Access public
- * @ Staticvar int
- **/
- Public static $ similarity = 80;
/** Enable the function corresponding to the image type
- * @ Access private
- * @ Staticvar string
- **/
- Private static $ _ createFunc = array (
- IMAGETYPE_GIF => 'imagecreatefromgif ',
- IMAGETYPE_JPEG => 'imagecreatefromjpeg ',
- IMAGETYPE_PNG => 'imagecreatefrompng ',
- IMAGETYPE_BMP => 'imagecreatefrombmp ',
- IMAGETYPE_WBMP => 'imagecreatefromwbmp ',
- IMAGETYPE_XBM => 'imagecreatefromxbm ',
- );
/** Create an image from a file
- * @ Param string $ filePath file address path
- * @ Return resource: If the image is enabled successfully, the image resource ID is passed. If the image fails, the value is false.
- **/
- Public static function createImage ($ filePath ){
- If (! File_exists ($ filePath) {return false ;}
/* Determine whether the file type can be enabled */
- $ Type = exif_imagetype ($ filePath );
- If (! Array_key_exists ($ type, self: $ _ createFunc) {return false ;}
$ Func = self ::$ _ createFunc [$ type];
- If (! Function_exists ($ func) {return false ;}
Return $ func ($ filePath );
- }
/** Hash image
- * @ Param resource $ src image resource ID
- * @ Return string the hash value of the image. If the image fails, the value is false.
- **/
- Public static function hashImage ($ src ){
- If (! $ Src) {return false ;}
/* Reduce the image size */
- $ Delta = 8 * self: $ rate;
- $ Img = imageCreateTrueColor ($ delta, $ delta );
- ImageCopyResized ($ img, $ src, 0, 0, 0, $ delta, $ delta, imagesX ($ src), imagesY ($ src ));
/* Calculate the gray-scale image value */
- $ GrayArray = array ();
- For ($ y = 0; $ y <$ delta; $ y ++ ){
- For ($ x = 0; $ x <$ delta; $ x ++ ){
- $ Rgb = imagecolorat ($ img, $ x, $ y );
- $ Col = imagecolorsforindex ($ img, $ rgb );
- $ Gray = intval ($ col ['red'] + $ col ['green'] + $ col ['blue'])/3) & 0xFF;
$ GrayArray [] = $ gray;
- }
- }
- Imagedestroy ($ img );
/* Calculate the gray-scale average of all pixels */
- $ Average = array_sum ($ grayArray)/count ($ grayArray );
/* Calculate the hash value */
- $ HashStr = '';
- Foreach ($ grayArray as $ gray ){
- $ HashStr. = ($ gray >=$ average )? '1': '0 ';
- }
- Return $ hashStr;
- }
/** Hash image file
- * @ Param string $ filePath file address path
- * @ Return string the hash value of the image. If the image fails, the value is false.
- **/
- Public static function hashImageFile ($ filePath ){
- $ Src = self: createImage ($ filePath );
- $ HashStr = self: hashImage ($ src );
- Imagedestroy ($ src );
Return $ hashStr;
- }
/** Compare two hash values. is it similar?
- * @ Param string $ the hash value of the aHash a image.
- * @ Param string $ hash value of the bHash B image
- * @ Return bool: If the image is similar, true is passed; otherwise, false is used.
- **/
- Public static function isHashSimilar ($ aHash, $ bHash ){
- $ AL = strlen ($ aHash); $ bL = strlen ($ bHash );
- If ($ aL! ==$ BL) {return false ;}
/* Calculate the number of allowable gaps */
- $ AllowGap = $ aL * (100-self: $ similarity)/100;
/* Calculate the Hamming distance between two hash values */
- $ Distance = 0;
- For ($ I = 0; $ I <$ aL; $ I ++ ){
- If ($ aHash {$ I }! ==$ BHash {$ I}) {$ distance ++ ;}
- }
Return ($ distance <= $ allowGap )? True: false;
- }
/** Compare two image files. is it similar?
- * @ Param string $ path of the aHash a image
- * @ Param string $ bHash B Image path
- * @ Return bool: If the image is similar, true is passed; otherwise, false is used.
- **/
- Public static function isImageFileSimilar ($ aPath, $ bPath ){
- $ AHash = ImageHash: hashImageFile ($ aPath );
- $ BHash = ImageHash: hashImageFile ($ bPath );
- Return ImageHash: isHashSimilar ($ aHash, $ bHash );
- }
- }
|