php比較圖片相似性程式碼範例

來源:互聯網
上載者:User
  1. /**

  2. * 圖片相似性比較
  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. /**取樣倍率 1~10
  18. * @access public
  19. * @staticvar int
  20. * */
  21. public static $rate = 2;

  22. /**相似性允許值 0~64

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

  27. /**圖片類型對應的開啟函數

  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. /**從檔案建立圖片

  40. * @param string $filePath 檔案地址路徑
  41. * @return resource 當成功開啟圖片則傳遞圖片 resource ID,失敗則是 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 圖片

  53. * @param resource $src 圖片 resource ID
  54. * @return string 圖片 hash 值,失敗則是 false
  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 圖片檔案

  83. * @param string $filePath 檔案地址路徑
  84. * @return string 圖片 hash 值,失敗則是 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. /**比較兩個 hash 值,是不是相似

  93. * @param string $aHash A圖片的 hash 值
  94. * @param string $bHash B圖片的 hash 值
  95. * @return bool 當圖片相似則傳遞 true,否則是 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. /**比較兩個圖片檔案,是不是相似

  110. * @param string $aHash A圖片的路徑
  111. * @param string $bHash B圖片的路徑
  112. * @return bool 當圖片相似則傳遞 true,否則是 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. }

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.