2015-04-03 66 views
2

同我试图同时使图像在我的服务器如何检测,如果图像名称不同,但图像是在PHP

1.如果图像名称来验证不同,但图像是相同的,然后什么也不做。

我怎样才能检测到

我的代码看起来是这样的

 if (file_exists($ficherof)) { 
      $x=file_get_contents($ficherof) ; 
      if($imagen['sha1']!=sha1($x)) 
      { 
       //do something 

      } 
     } 
     else 
//here I have to check if the file name is different but the files are same then donot upload 
     file_put_contents($ficherof, $contenido); 
+1

获取两个文件内容并进行比较,使用'sha1_file()'进行比较,速度要快得多。 – Daan 2015-04-03 12:07:11

+0

@Daan抱歉,它的图像混乱,所以我应该怎么做,在这方面 – Vikram 2015-04-03 12:08:52

+1

有一个很好的答案[这里](http://stackoverflow.com/a/29253608/1685196)。 – Michel 2015-04-03 12:10:46

回答

3

检查sha和两个图像,以便对它们进行比较是不正确的!您可以将两个相同的文件与不同的sha总和相加 - 因为可以将日期/时间嵌入到EXIF数据,PNG标头或TIFF标签中。

做正确的是我回答的方式... here

关注此。我创建了2个IDENTICAL黑色方块并对它们进行比较:

convert -size 256x256 xc:black 1.png 
convert -size 256x256 xc:black 2.png 
diff [12].png 
Binary files 1.png and 2.png differ 

它们为什么不同?因为日期是在其中:

identify -verbose 1.png 2.png | grep date 
date:create: 2015-04-03T13:31:30+01:00 
date:modify: 2015-04-03T13:31:30+01:00 
date:create: 2015-04-03T13:31:33+01:00 
date:modify: 2015-04-03T13:31:33+01:00 

如果我创建了一个256×256的黑色正方形GIF和相同的256x256的黑PNG?

convert -size 256x256 xc:black 1.png 
convert -size 256x256 xc:black 1.gif 

,看看他们,他们是不同的尺寸,并会显然有不同的校验

ls -l 1* 
-rw-r--r-- 1 mark staff 392 3 Apr 13:34 1.gif 
-rw-r--r-- 1 mark staff 279 3 Apr 13:34 1.png 

但ImageMagick的正确告诉我有ZERO像素不同

convert -metric ae 1.png 1.gif -compare -format "%[distortion]" info: 
0 

当然,如果你比较sha和他们是相同的,那么图像是相同的。我所说的是即使它们的校验和不同,图像也可能是相同的。