Php comparison image similarity code example

Source: Internet
Author: User
Tags scale image
Php comparison image similarity code example

  1. /**

  2. * Image similarity comparison
  3. *
  4. * @ Version $ Id: ImageHash. php 4429 2012-04-17 13: 20: 31Z jajax $
  5. * @ Authorjax. hu
  6. * Www.osxue.com
  7. * // Sample_1
  8. * $ AHash = ImageHash: hashImageFile('wsz.11.jpg ');
  9. * $ BHash = ImageHash: hashImageFile('wsz.12.jpg ');
  10. * Var_dump (ImageHash: isHashSimilar ($ aHash, $ bHash ));
  11. *
  12. * // Sample_2
  13. * Var_dump (ImageHash: isImageFileSimilar('wsz.11.jpg ', 'wsz.12.jpg '));
  14. *
  15. */

  16. Class ImageHash {

  17. /** Sampling rate: 1 ~ 10
  18. * @ Access public
  19. * @ Staticvar int
  20. **/
  21. Public static $ rate = 2;

  22. /** Allowed similarity values: 0 ~ 64

  23. * @ Access public
  24. * @ Staticvar int
  25. **/
  26. Public static $ similarity = 80;

  27. /** Enable the function corresponding to the image type

  28. * @ Access private
  29. * @ Staticvar string
  30. **/
  31. Private static $ _ createFunc = array (
  32. IMAGETYPE_GIF => 'imagecreatefromgif ',
  33. IMAGETYPE_JPEG => 'imagecreatefromjpeg ',
  34. IMAGETYPE_PNG => 'imagecreatefrompng ',
  35. IMAGETYPE_BMP => 'imagecreatefrombmp ',
  36. IMAGETYPE_WBMP => 'imagecreatefromwbmp ',
  37. IMAGETYPE_XBM => 'imagecreatefromxbm ',
  38. );

  39. /** Create an image from a file

  40. * @ Param string $ filePath file address path
  41. * @ Return resource: If the image is enabled successfully, the image resource ID is passed. If the image fails, the value is false.
  42. **/
  43. Public static function createImage ($ filePath ){
  44. If (! File_exists ($ filePath) {return false ;}

  45. /* Determine whether the file type can be enabled */

  46. $ Type = exif_imagetype ($ filePath );
  47. If (! Array_key_exists ($ type, self: $ _ createFunc) {return false ;}

  48. $ Func = self ::$ _ createFunc [$ type];

  49. If (! Function_exists ($ func) {return false ;}

  50. Return $ func ($ filePath );

  51. }

  52. /** Hash image

  53. * @ Param resource $ src image resource ID
  54. * @ Return string the hash value of the image. If the image fails, the value is false.
  55. **/
  56. Public static function hashImage ($ src ){
  57. If (! $ Src) {return false ;}

  58. /* Reduce the image size */

  59. $ Delta = 8 * self: $ rate;
  60. $ Img = imageCreateTrueColor ($ delta, $ delta );
  61. ImageCopyResized ($ img, $ src, 0, 0, 0, $ delta, $ delta, imagesX ($ src), imagesY ($ src ));

  62. /* Calculate the gray-scale image value */

  63. $ GrayArray = array ();
  64. For ($ y = 0; $ y <$ delta; $ y ++ ){
  65. For ($ x = 0; $ x <$ delta; $ x ++ ){
  66. $ Rgb = imagecolorat ($ img, $ x, $ y );
  67. $ Col = imagecolorsforindex ($ img, $ rgb );
  68. $ Gray = intval ($ col ['red'] + $ col ['green'] + $ col ['blue'])/3) & 0xFF;

  69. $ GrayArray [] = $ gray;

  70. }
  71. }
  72. Imagedestroy ($ img );

  73. /* Calculate the gray-scale average of all pixels */

  74. $ Average = array_sum ($ grayArray)/count ($ grayArray );

  75. /* Calculate the hash value */

  76. $ HashStr = '';
  77. Foreach ($ grayArray as $ gray ){
  78. $ HashStr. = ($ gray >=$ average )? '1': '0 ';
  79. }
  80. Return $ hashStr;
  81. }

  82. /** Hash image file

  83. * @ Param string $ filePath file address path
  84. * @ Return string the hash value of the image. If the image fails, the value is false.
  85. **/
  86. Public static function hashImageFile ($ filePath ){
  87. $ Src = self: createImage ($ filePath );
  88. $ HashStr = self: hashImage ($ src );
  89. Imagedestroy ($ src );

  90. Return $ hashStr;

  91. }

  92. /** Compare two hash values. is it similar?

  93. * @ Param string $ the hash value of the aHash a image.
  94. * @ Param string $ hash value of the bHash B image
  95. * @ Return bool: If the image is similar, true is passed; otherwise, false is used.
  96. **/
  97. Public static function isHashSimilar ($ aHash, $ bHash ){
  98. $ AL = strlen ($ aHash); $ bL = strlen ($ bHash );
  99. If ($ aL! ==$ BL) {return false ;}

  100. /* Calculate the number of allowable gaps */

  101. $ AllowGap = $ aL * (100-self: $ similarity)/100;

  102. /* Calculate the Hamming distance between two hash values */

  103. $ Distance = 0;
  104. For ($ I = 0; $ I <$ aL; $ I ++ ){
  105. If ($ aHash {$ I }! ==$ BHash {$ I}) {$ distance ++ ;}
  106. }

  107. Return ($ distance <= $ allowGap )? True: false;

  108. }

  109. /** Compare two image files. is it similar?

  110. * @ Param string $ path of the aHash a image
  111. * @ Param string $ bHash B Image path
  112. * @ Return bool: If the image is similar, true is passed; otherwise, false is used.
  113. **/
  114. Public static function isImageFileSimilar ($ aPath, $ bPath ){
  115. $ AHash = ImageHash: hashImageFile ($ aPath );
  116. $ BHash = ImageHash: hashImageFile ($ bPath );
  117. Return ImageHash: isHashSimilar ($ aHash, $ bHash );
  118. }
  119. }

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.