【问题标题】:Broken Image Replace using http response headers使用 http 响应标头替换损坏的图像
【发布时间】:2014-03-21 21:44:12
【问题描述】:

我想隐藏损坏的图像图标,但我使用的图像服务器不会抛出 404 页面,而是将其重定向到它们的替代图像,所以我不能使用传统的 onerror 方法。 如果服务器抛出 404 页面作为响应,此方法效果很好。

<html>
<body> 
<div><img src="http://stackoverflow.com/image.jpg" alt="image" onerror="this.style.display='none'"/></div>
</body>
</html>

但在图像 URL 为:http://tinypic.com/someimage.jpg 的情况下 上述方法行不通。因为这是一个 302 响应,它会将其重定向到其他图像。我也想隐藏它。 是否可以以某种方式使用 http 响应作为显示/隐藏图像的条件。

这是上图的http头:

$ curl -I http://tinypic.com/notfound.jpg
HTTP/1.1 302 Found
Server: Apache
Location: http://tinypic.com/images/404.gif
Expires: Fri, 21 Mar 2014 21:33:07 GMT
Cache-Control: max-age=300
Content-Type: text/html; charset=iso-8859-1
Content-Length: 217
Accept-Ranges: bytes
Date: Fri, 21 Mar 2014 21:28:07 GMT
X-Varnish: 1535787681
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Varnish-Server: den2tpv01
X-Cache: MISS

现在,我认为可以以某种方式使用 HTTP/1.1 302 Found。有什么建议吗?

【问题讨论】:

    标签: image http http-headers


    【解决方案1】:

    来吧,简单的解决方案:

    <img src="www.google.com/noanyimage.png" width="200px" height ="200px" onerror="this.src ='';" />
    

    【讨论】:

      【解决方案2】:

      这里是使用php的解决方案...

      <?PHP 
      $url = "http://tinypic.com/notfound.jpg"; //Image link here
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $html = curl_exec($ch);
      $status_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
      
      if($status_code==302 or $status_code==301 or $status_code==404){
        echo "no image found"; 
      }
      else {
      echo '<img src="'.$url.'" />';  
      }
      curl_close($ch);
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2014-08-08
        • 2012-02-09
        • 2015-12-11
        • 2012-10-23
        • 2010-09-10
        • 1970-01-01
        • 1970-01-01
        • 2014-06-28
        相关资源
        最近更新 更多