检查图像是否相同

时间:2012-07-14 16:32:09

标签: php image image-processing

我有一个嵌入Imgur.com图片的网站,如果显示默认的imgur错误图片,我想要一个显示自定义404图像的代码。

我要检查的错误图像是否显示为http://i.imgur.com/ffffffffffff.jpg

2 个答案:

答案 0 :(得分:3)

您可以散列文件然后进行比较:

if(md5_file($file1) === md5_file($file2)) 
{
    // the same images
} 
else 
{
    // different images
}

您可以使用相对或绝对路径。


如何检查是否显示默认错误图像?

if(md5_file("http://i.imgur.com/ffffffffffff.jpg") === md5_file("http://i.imgur.com/some-other-non-existent-image.jpg"))
{
    //display your error image
} 

答案 1 :(得分:0)

只是要知道,如果无法从链接中找到imgur图像,该链接将始终使用以下消息加载自定义图像:

the image you are requesting does not exist or is no longer available.

因此,如果找不到图像,它将被imgur中的自定义图像替换。

问题在图片不再可用,无法加载或其他事情发生时开始,然后您将获得如下典型的损坏图像:

enter image description here

<强>代码:

嗯,为了解决这个问题,我为你做了这个例子:

(我假设为了获取imgur图像,你从imgur用户下载源代码然后恢复所有图像链接并将它们放入数组中。这就是为什么我从那时开始)

<?php
/* Array with Imgur images */
$imgArray = array('http://i.imgur.com/Uqotub.jpg', 'http://i.imgur.com/kPQFgb.jpg', 
'http://i.imgur.com/7teVGb.jpg', 'http://i.imgur.com/lnCZIb.jpg', 'http://i.imgur.com/sn6Ayb.jpg');

/* Imgur custom 404 image */
$customImg = 'http://i.imgur.com/404.jpg';

/* Do a loop for your images, if a valid image was found and loaded, 
 * you will have the following array from imgData: 
 * Array ( [0] => 160 [1] => 160 [2] => 2 [3] => width="160" height="160" [bits] => 8 [channels] => 3 [mime] => image/jpeg )
 */
foreach ($imgArray as $index => $value) {

    $imgData = getimagesize($value);

    /* if $imgData is an array means that image loaded ok */
    if(is_array($imgData)) {
        echo '<img src="'.$value.'"/>';
    /* else $imgData is nothing because image is broken or not available, 
     * now you pass the custom image */
    } else {
        echo '<img src="'.$customImg.'"/>';
    }
}
?>

<强>结果:

enter image description here

PS。我的问题不明确。所以,如果你想检查图像是否相同或其他什么,你可以做Nikola K.在他/她的帖子中回答的问题。

希望这会有所帮助: - )

相关问题