JAVA中的原理移栽到PHP中

WBOY
Release: 2016-06-13 13:00:45
Original
785 people have browsed it

JAVA中的原理移植到PHP中
一下是我在CSDN上下载的一段JAVA的源代码。目的是计算出图片指纹,
public static String produceFingerPrint(String filename) {
BufferedImage source = ImageHelper.readPNGImage(filename);// 读取文件

int width = 8;
int height = 8;

// 第一步,缩小尺寸。
// 将图片缩小到8x8的尺寸,总共64个像素。这一步的作用是去除图片的细节,只保留结构、明暗等基本信息,摒弃不同尺寸、比例带来的图片差异。
BufferedImage thumb = ImageHelper.thumb(source, width, height, false);

// 第二步,简化色彩。
// 将缩小后的图片,转为64级灰度。也就是说,所有像素点总共只有64种颜色。
int[] pixels = new int[width * height];
for (int i = 0; i  for (int j = 0; j  pixels[i * height + j] = ImageHelper.rgbToGray(thumb.getRGB(i, j));
}
}

// 第三步,计算平均值。
// 计算所有64个像素的灰度平均值。
int avgPixel = ImageHelper.average(pixels);

// 第四步,比较像素的灰度。
// 将每个像素的灰度,与平均值进行比较。大于或等于平均值,记为1;小于平均值,记为0。
int[] comps = new int[width * height];
for (int i = 0; i  if (pixels[i] >= avgPixel) {
comps[i] = 1;
} else {
comps[i] = 0;
}
}

// 第五步,计算哈希值。
// 将上一步的比较结果,组合在一起,就构成了一个64位的整数,这就是这张图片的指纹。组合的次序并不重要,只要保证所有图片都采用同样次序就行了。
StringBuffer hashCode = new StringBuffer();
for (int i = 0; i  int result = comps[i] * (int) Math.pow(2, 3) + comps[i + 1] * (int) Math.pow(2, 2) + comps[i + 2] * (int) Math.pow(2, 1) + comps[i + 2];
hashCode.append(binaryToHex(result));
}

// 得到指纹以后,就可以对比不同的图片,看看64位中有多少位是不一样的。
return hashCode.toString();
}


现在我要用PHP做网页也是实现这种功能,问哪位高手能给出转换到PHP的源代码呢?
------解决方案--------------------
算法描述的很清楚,实现起来就不难
需要注意的是 php 不支持 64位 的整型数,所以函数提供了几种可能的返回样式

/**<br>
 * 生成指纹<br>
 * $filename 图片文件名,可以是URL。只能是GD支持的图片类型<br>
 * $retmode 返回格式:0 二进制表示 1 数组 2 十六进制表示<br>
 **/<br>
function produceFingerPrint($filename, $retmode=0) {<br>
  $sim = imagecreatefromstring( file_get_contents($filename) );<br>
  $dim = imagecreate(8, 8);<br>
  imagecopyresized($dim, $sim, 0, 0, 0, 0, 8, 8, imagesx($sim), imagesy($sim));<br>
  imagetruecolortopalette($sim, true, 64);<br>
  //imagefilter($sim, IMG_FILTER_GRAYSCALE);<br>
<br>
  for($x=0; $x
    $c = imagecolorat($dim, $x, $y);<br>
    $p[] = array_sum( imagecolorsforindex($dim, $c))/3;<br>
  }<br>
  $avl = array_sum($p)/count($p);<br>
  $r = '';<br>
  foreach($p as $v) $r .= $v>=$avl ? 1 : 0;<br>
<br>
  if($retmode == 0) return $r;<br>
  $p = array_map('bindec', str_split($r, 8));<br>
  if($retmode == 1) return $p;<br>
  if($retmode == 2) { <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!