Comparison of similarity between simple PHP versions of images

Source: Internet
Author: User
Tags scale image
Comparison of similarity between simple PHP versions of images ,. The simple version of PHP implements image similarity comparison. because the API implemented by php for similar image search is not very suitable for my purposes, I need to redefine the API architecture, to compare the similarity between simple version images implemented by PHP,

Because the php API implemented by similar image search is not very suitable for my purposes, I have to redefine the API architecture and rewrite it to a simple function method, although it is still packaged using objects.

The code is as follows:


<? Php
/**
* Image similarity comparison
*
* @ Version $ Id: ImageHash. php 4429 2012-04-17 13: 20: 31Z jajax $
* @ Author jax. hu
*
*
* //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 );
}

}

Because the php API implemented by similar image search is not very suitable for my purposes, I have to redefine the API architecture and rewrite it to a relatively simple one...

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.