程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> php 圖片相似度 實現教程

php 圖片相似度 實現教程

編輯:PHP基礎知識
 

作者僅僅根據原意略改代碼並加上注釋
<?php

/**
* 圖片比較類
*
*/
class ImgCompare {

/**單例,防止重復初始化*/
private static $_instance = null;

/**誤差*/
public static $rate = 1;

/**
* 初始化一個實例
*/
public static function init() {

if (self::$_instance === null) {
self::$_instance = new self();
}

return self::$_instance;
}

/**
* 獲取兩個圖片的
*/
public function doCompare($file) {
if(!function_exists('imagecreatetruecolor')) {
throw new Exception('GD Library must be load if you want to use the class ImgCompare');
}

$is_string = false;

if(is_string($file)) {
$file = array($file);
$is_string = true;
}

$result = array();
foreach ($file as $f) {
$result[] = $this->hash($f);
}

return $is_string ? $result[0] : $result;
}

/**
* 計算漢明距離
*/
public function checkIsSimilar($img_hash_1,$img_hash_2) {
if (file_exists($img_hash_1) && file_exists($img_hash_2)) {
$img_hash_1 = self::doCompare($img_hash_1);
$img_hash_2 = self::doCompare($img_hash_2);
}

if(strlen($img_hash_1) !== strlen($img_hash_2)) {
return false;
}

$count = 0;
$len = strlen($img_hash_1);
for ($i=0;$i<$len;$i++) {
if($img_hash_1{$i} !== $img_hash_2{$i}) {
// 計算 有多少位是不一樣的
$count ++;
}
}

// 得到指紋以後,就可以對比不同的圖片,看看64位中有多少位是不一樣的。在理論上,這等同於計算"漢明距離"(Hamming distance)。
// 如果不相同的數據位不超過5*誤差,就說明兩張圖片很相似;如果大於10*誤差,就說明這是兩張不同的圖片。

return $count <= (5*(self::$rate)*(self::$rate))?true:false;
}

/**
* 將圖片縮小到 8x8*誤差的平方 的尺寸,總共64*誤差的平方個像素。
* 這一步的作用是去除圖片的細節,只保留結構、明暗等基本信息,摒棄不同尺寸、比例帶來的圖片差異。
*/
public function hash($file) {
if (!file_exists($file)) {
return false;
}

$height = 8*self::$rate;
$width = 8*self::$rate;

$img = imagecreatetruecolor($width, $height);

list($w,$h) = getimagesize($file);
$source = self::createImg($file);

// 重采樣拷貝部分圖像並調整大小
// 將一幅圖像中的一塊正方形區域拷貝到另一個圖像中,平滑地插入像素值,因此,尤其是,減小了圖像的大小而仍然保持了極大的清晰度
// 如果源和目標的寬度和高度不同,則會進行相應的圖像收縮和拉伸。坐標指的是左上角
// 本函數可用來在同一幅圖內部拷貝(如果 dst_image 和 src_image 相同的話)區域,但如果區域交迭的話則結果不可預知。
imagecopyresampled($img, $source, 0, 0, 0, 0, $width, $height, $w, $h);
$value = self::getHashValue($img);
imagedestroy($img);

return $value;
}

public function getHashValue($img) {
$width = imagesx($img);
$height = imagesy($img);

$total = 0;
$array = array();

// 將縮小後的圖片,轉為64級灰度。也就是說,所有像素點總共只有64種顏色。
for ($y =0;$y<$height;$y++) {
for ($x=0;$x<$width;$x++) {
// 獲取 指定的圖形中指定坐標像素的顏色索引值
// 將縮小的圖像轉為64級灰度
$gray = (imagecolorat($img, $x, $y) >> 8) & 0xFF;
if (!is_array($array[$y])) {
$array[$y] = array();
}

$array[$y][$x] = $gray;
$total += $gray;
}
}

// 獲取灰度平均值
$average = intval($total/(64*self::$rate*self::$rate));
$result = '';

for ($y=0;$y<$height;$y++) {
for ($x=0;$x<$width;$x++) {
// 將每個像素的灰度,與平均值進行比較。大於或等於平均值,記為1;小於平均值,記為0
if ($array[$y][$x] >= $average) {
$result .= '1';
} else {
$result .= '0';
}
}
}

return $result;
}

/**
* 生成圖片
*/
public function createImg($file) {
$ext = self::getFileExt($file);
if ($ext === 'jpeg') $ext = 'jpg';
$img = null;
switch ($ext){
case 'png' : $img = imagecreatefrompng($file);break;
case 'jpg' : $img = imagecreatefromjpeg($file);break;
case 'gif' : $img = imagecreatefromgif($file);break;
default:break;
}
return $img;
}

/**
* 獲取圖片擴展名
*/
public function getFileExt($file){
$infos = explode('.', $file);
$ext = strtolower($infos[count($infos) - 1]);
if (!in_array($ext,array('jpg','jpeg','png','gif'))) {
throw new Exception("file extension must in 'jpg','jpeg','png','gif' ");
exit;
}

return $ext;
}
}

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved