Created
June 27, 2012 12:36
-
-
Save mikespook/3003825 to your computer and use it in GitHub Desktop.
检测图片内容是否匹配
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 获得图片的采样 | |
function samplingGif($filename) { | |
$sampling = array(); | |
$img = imagecreatefromgif($filename); | |
$size = getimagesize($filename); | |
$w = $size[0]; | |
$h = $size[1]; | |
for($x = 0; $x < $w; $x++) { | |
$sampling[$x] = array(); | |
for($y = 0; $y < $h; $y ++) { | |
$sampling[$x][$y] = imagecolorat($img, $x , $y); | |
} | |
} | |
return $sampling; | |
} | |
// 用图片匹配采样,如果大于阀值,说明相等。 | |
function testGif($filename, $sampling, $threshold = 0.8) { | |
$r = 0; | |
$img = imagecreatefromgif($filename); | |
$size = getimagesize($filename); | |
$w = $size[0]; | |
$h = $size[1]; | |
for($x = 0; $x < $w; $x++) { | |
for($y = 0; $y < $h; $y ++) { | |
$r += $sampling[$x][$y] == imagecolorat($img, $x , $y) ? 1 : 0; | |
} | |
} | |
return $r / ($w * $h) > $threshold; | |
} | |
// 1083.gif 为已知图片,显示为 5 | |
$s = samplingGif('1083.gif'); | |
// 10250.gif 为未知图片,显示为 5 | |
var_dump(testGif('10250.gif', $s)); | |
// 19523.gif 为未知图片,显示为 2 | |
var_dump(testGif('19523.gif', $s)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment