Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
检测图片内容是否匹配
<?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
You can’t perform that action at this time.