比较两张图片是否为同一文件很容易:使用文件的MD5值即可。但是,通过使用PHP GD获取两张图片的差异来确定它们是否相同,这种方法是否可行呢?如果我们获取了两张图片的差异,并且它全部是白色(我会假设是白色或者黑色),那么我们现在就知道它们是否是同一张照片了吗?
另外,我想知道是否有可能获得两张大小相等的图片,以创建洋葱皮效果:其中一张透明度为50%,另一张也是50%。
另外,我想知道是否有可能获得两张大小相等的图片,以创建洋葱皮效果:其中一张透明度为50%,另一张也是50%。
大多数其他答案是在提及使用各种哈希函数。而这个问题明确地询问的是比较图像内容,而不是比较文件。
这意味着您需要真正理解图像的内容。在 PHP 中,通常使用两个扩展来处理此问题:ImageMagick 和 GD。
ImageMagick 提供了各种工具,可以通过 PHP ImageMagick 扩展进行使用。
http://www.php.net/manual/en/function.imagick-compareimages.php
最大的问题是该库的文档几乎不存在,因此需要进行大量的尝试和错误检查。PHP 扩展是 ImageMagick 库的一个很薄的封装器,因此 compareimages() 函数的详细信息可以在 ImageMagick 文档中找到。
$md5image1 = md5(file_get_contents($image1));
$md5image2 = md5(file_get_contents($image2));
if ($md5image1 == $md5image2) {
}
这个类似的问题在 Stackoverflow 的讨论中被提出,我曾经开发了一个用于自己使用的工具。现在在这里分享出来以帮助其他人。
它需要两个(或多个)图像,并提供了一些选项来检查它们之间的差异。可以选择使用的分辨率和严格程度等选项。
我还写了一篇更全面的博客文章介绍了它的使用方法。
<?php
define('FILENAME1','./image1.png');
define('FILENAME2','./image2.png');
define('OUTEXT','png'); //Output extension
/**
* Calculate an arbitrary metric of difference between images
*
* @param resource(gd) $img1 First image
* @param resource(gd) $img2 Second image
* @param int $resX Scaled width resolution
* @param int $resY Scaled height resolution
* @param int $pow Mean square error if 2, but could be another power
* @param string $channel RGB channel or 'all' for perceived luminance
*
* @return float the calculated metric
*/
function diff($img1,$img2,$resX=false,$resY=false,$pow='2',$channel='all'){
//Scaling to image 1 size for default
if(!$resX||!$resY){
$resX=imagesx($img1); //width
$resY=imagesy($img2); //height
}
//Use imagescale() function to scale the images
$thumb1=imagescale($img1,$resX,$resY);
$thumb2=imagescale($img2,$resX,$resY);
//Sum of errors
$sum=0;
for($x=0;$x<$resX;++$x){
for($y=0;$y<$resY;++$y){
//Read pixel bytes
$bytes1=imagecolorat($thumb1,$x,$y);
$bytes2=imagecolorat($thumb2,$x,$y);
//Split the channel values from bytes
$colors1=imagecolorsforindex($thumb1,$bytes1);
$colors2=imagecolorsforindex($thumb2,$bytes2);
//Choose image channel
if($channel=='all'){
//Perceived luminance
$value1=sqrt(0.2126*$colors1['red']**2+0.7152*$colors1['green']**2+ 0.0722*$colors1['blue']**2);
$value2=sqrt(0.2126*$colors2['red']**2+0.7152*$colors2['green']**2+ 0.0722*$colors2['blue']**2);
}else{
//RGB channel
$value1=$colors1[$channel];
$value2=$colors2[$channel];
}
$sum+=abs($value1-$value2)**$pow;
}
}
//Return mean of the error sum
return $sum/($resX*$resY);
}
//Show image in HTML
function imgdraw($imgobj){
//Choose function
$image="image".OUTEXT;
//Capture the image data stream
ob_start();
$image($imgobj);
$data = ob_get_clean();
//Create and HTML img
echo '<img src="data:image/png;base64,'.base64_encode($data).'" />';
}
//Load an image
function loadimg($filename){
//Get filename extension
$ext=substr($filename,strrpos($filename,'.')+1);
//Create image object
$imagecreate="imagecreatefrom$ext";
return $imagecreate($filename);
}
//Test
$img1=loadimg(FILENAME1);
$img2=loadimg(FILENAME2);
if( !$img1 || !$img2){
//Problem reading the files
die("Error loading image files");
}else{
imgdraw($img1);
imgdraw($img2);
}
//Times for 133x144 pixels png images
echo "<p>original size MSE perceived luminance: ".diff($img1,$img2)."</p>";
//time: 0.2281 seconds.
echo "<p>original size MSE green channel: ".diff($img1,$img2,false,false,2,'green')."</p>";
//time: 0.1364 seconds.
echo "<p>original size linear perceived luminance: ".diff($img1,$img2,false,false,1)."</p>";
//time: 0.1920 seconds.
echo "<p>original size linear green channel: ".diff($img1,$img2,false,false,1,'green')."</p>";
//time: 0.1351 seconds.
echo "<p>64x64 MSE perceived luminance: ".diff($img1,$img2,64,64)."</p>";
//time: 0.0431 seconds.
echo "<p>64x64 MSE green channel: ".diff($img1,$img2,64,64,2,'green')."</p>";
//time: 0.0293 seconds.
echo "<p>64x64 linear perceived luminance: ".diff($img1,$img2,64,64,1)."</p>";
//time: 0.0423 seconds.
echo "<p>64x64 linear green channel: ".diff($img1,$img2,64,64,1,'green')."</p>";
//time: 0.0293 seconds.
echo "<p>16x16 MSE perceived luminance: ".diff($img1,$img2,16,16)."</p>";
//time: 0.0028 seconds.
echo "<p>16x16 MSE green channel: ".diff($img1,$img2,16,16,2,'green')."</p>";
//time: 0.0027 seconds.
echo "<p>16x16 linear perceived luminance: ".diff($img1,$img2,16,16,1)."</p>";
//time: 0.0027 seconds.
echo "<p>16x16 linear green channel: ".diff($img1,$img2,16,16,1,'green')."</p>";
//time: 0.0018 seconds.
?>
使用GD库在PHP中进行图像比较功能 http://www.robert-lerner.com/imagecompare.php
如果你只是比较两个文件,那么哈希数据然后进行比较是完美的解决方案。如果你要比较大量的文件,最好先按大小排序,然后只与相同大小的文件进行比较。
$comparator = new SapientPro\ImageComparator\ImageComparator();
$similarity = $imageComparator->compare('your-images/your-image1.jpg', 'your-images/your-image12.jpg');
echo $similarity; // 82.3