Home  >  Article  >  Backend Development  >  PHP image similarity code example

PHP image similarity code example

WBOY
WBOYOriginal
2016-07-25 08:52:061866browse
  1. /**

  2. * Image similarity comparison
  3. *
  4. * @version $Id: ImageHash.php 4429 2012-04-17 13:20:31Z jax $
  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. /**Similarity allowed value 0~64

  23. * @access public
  24. * @staticvar int
  25. **/
  26. public static $similarity = 80;

  27. /**The opening 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 When the image is successfully opened, the image resource ID is passed, and if it fails, it is false
  42. **/
  43. public static function createImage($filePath){
  44. if(!file_exists($filePath)){ return false; }

  45. /*判断文件类型是否可以开启*/

  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 image hash value, false on failure
  55. **/
  56. public static function hashImage($src){
  57. if(!$src){ return false; }

  58. /*缩小图片尺寸*/

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

  62. /*计算图片灰阶值*/

  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. /*计算所有像素的灰阶平均值*/

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

  75. /*计算 hash 值*/

  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 image hash value, false on failure
  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 ​​to see if they are similar

  93. * @param string $aHash The hash value of picture A
  94. * @param string $bHash The hash value of picture B
  95. * @return bool If the pictures are similar, true will be passed, otherwise false
  96. **/
  97. public static function isHashSimilar($aHash, $bHash){
  98. $aL = strlen($aHash); $bL = strlen($bHash);
  99. if ($aL !== $bL){ return false; }

  100. /*计算容许落差的数量*/

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

  102. /*计算两个 hash 值的汉明距离*/

  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 to see if they are similar

  110. * @param string $aHash The path of image A
  111. * @param string $bHash The path of image B
  112. * @return bool When the images are similar, true is passed, otherwise false
  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. }>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn