$source_image = imagecreatefromjpeg('image1.jpg');
$compare_image = imagecreatefromjpeg('image2.jpg');
$width = imagesx($source_image);
$height = imagesy($source_image);
$difference_image = imagecreatetruecolor($width, $height);
$difference = 0;
for($y = 0; $y < $height; $y++) {
for($x = 0; $x < $width; $x++) {
$rgb1 = imagecolorat($source_image, $x, $y);
$r1 = ($rgb1 >> 16) & 0xFF;
$g1 = ($rgb1 >> 8) & 0xFF;
$b1 = $rgb1 & 0xFF;
$rgb2 = imagecolorat($compare_image, $x, $y);
$r2 = ($rgb2 >> 16) & 0xFF;
$g2 = ($rgb2 >> 8) & 0xFF;
$b2 = $rgb2 & 0xFF;
$difference += abs($r1 - $r2) + abs($g1 - $g2) + abs($b1 - $b2);
$color = abs($rgb1 - $rgb2);
imagesetpixel($difference_image, $x, $y, $color);
}
}
$percent = round(100 - ($difference / (255 * $width * $height)) * 100, 2);
echo "Images are {$percent}% similar.";
上一篇:比较图片来源(if语句)
下一篇:比较图像来源