2012-07-14 166 views
-2

我有一个嵌入来自Imgur.com的图像的网站,如果显示默认imgur错误图像,我想要一个将显示自定义404图像的代码。检查图像是否相同

,我要检查,如果它显示的错误图像http://i.imgur.com/ffffffffffff.jpg

+0

尝试,并帮助人们帮助你通过提供图像和URL的例子... – Layke 2012-07-14 16:34:21

+0

您可以生成404图像文件的MD5和比较。 – 2012-07-14 16:34:31

+0

你到底想做什么?看看图像是否相同,然后如果他们是相同的显示自定义404图像或...你只想把自定义404图像,如果你的图像之一无法加载或无法找到 – 2012-07-14 16:54:41

回答

2

可以散列文件,然后对它们进行比较:

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 
} 
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.在他/她的帖子中回答的内容。

希望这有助于:-)