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

Vik*_*han 3 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);
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 5

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

正确的方法在我的回答中......在这里

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

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

如果我创建一个 256x256 的黑色方形 GIF 和一个相同的 256x256 黑色 PNG 会怎样?

convert -size 256x256 xc:black 1.png
convert -size 256x256 xc:black 1.gif
Run Code Online (Sandbox Code Playgroud)

看看它们,它们的大小不同,并且显然会有不同的校验和

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
Run Code Online (Sandbox Code Playgroud)

但 ImageMagick 正确地告诉我有零个像素不同

convert -metric ae 1.png 1.gif -compare -format "%[distortion]" info:
0
Run Code Online (Sandbox Code Playgroud)

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