php - 如何检测图像名称是否不同但图像在php中相同

标签 php file image-processing

我在将图像放入我的服务器时尝试进行验证

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);

最佳答案

检查两个图像的 sha 总和以比较它们是不正确的!您可以拥有两个具有不同 sha 总和的相同文件 - 因为日期/时间可以嵌入到 EXIF 数据、PNG header 或 TIFF 标签中。

正确的方法在我的回答中... here

看这个。我创建了 2 个相同的黑色方 block 并比较它们:

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

如果我创建一个 256x256 黑色方形 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 正确地告诉我有零个像素不同

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

当然,如果您比较 sha 总和并且它们相同,则图像是相同的。我要说的是,即使校验和不同,图像也可能相同。

关于php - 如何检测图像名称是否不同但图像在php中相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29431858/

相关文章:

php 函数回显 javascript 字符串

php - $this-> 委托(delegate)对象上的 Netbeans 自动完成

C# - Windows 窗体应用程序 - 保存文件

image-processing - 一种快速细化算法

无法识别 php 变量。

php - 在 php 中创建递归函数来重新构造记录集?

c# - 检查服务器上是否存在文件

android - Android 上的 JavaRx : Observe if multiple files have downloaded

python - 如何在 Keras 中重用 VGG19 进行图像分类?

java - 合并两个图像 --> 4 倍大小!如何减小文件大小?