用自定义图像替换破碎的外部图像 [英] Replacing Broken External Images With Custom Image

查看:107
本文介绍了用自定义图像替换破碎的外部图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历在外部网站上托管的图像的URL字符串数组。

I'm looping through an array of URL strings of images hosted at an external site.

它看起来像这样:

def get_image_urls
  image_url_array.each do |image_url|
    puts image_tag image_url
  end
end

这将返回托管在外部网站上的图像的URL。问题是,其中一些图像可能会被破坏(404)。例如:

Which will return the URLs of images hosted on the external site. The problem is, some of these images might be broken (404). So for example:

get_image_urls
# These would return image_tags, but for brevity...
=> "http://someothersite.com/images/1.jpg"
   "http://someothersite.com/images/2.jpg"
   "http://someothersite.com/images/3.jpg" # <-- (Broken: 404)
   "http://someothersite.com/images/4.jpg"
   "http://someothersite.com/images/5.jpg" # <-- (Broken: 404)

我要做的是更换已损坏图像的URL字符串为我自己网站上托管的缺失图像。所以使用上面的例子,3.jpg和5.jpg被打破了,我想要返回这样的东西:

What I'm looking to do is replace the URL strings of the broken images to a "missing" image hosted on my own site. So using the example above, with 3.jpg and 5.jpg being broken, I want to have returned something like this:

get_image_urls
# These would return image_tags, but for brevity...
=> "http://someothersite.com/images/1.jpg"
   "http://someothersite.com/images/2.jpg"
   "http://mysite.com/images/missing.png"
   "http://someothersite.com/images/4.jpg"
   "http://mysite.com/images/missing.png"

有没有一种简单的方法可以解决这个问题?非常感谢提前。

Is there a simple way to solve this problem? Thanks so much in advance.

推荐答案

你能不能对图像做一个简单的请求并检查它是否是404?它不完美,但会占上风。取决于你运行它的频率。如果您无法直接访问服务器上的文件进行检查,那么HTTP请求是唯一的方法。对于速度,您可以执行仅标头请求。将需要一个代码示例,让我挖出一个...

Can't you do a simple request for the image and check if its a 404? Its not perfect but would catch the bulk. Depends how often you are running it. If you don't have access to the files on the server directly to check then a HTTP request is the only way. For speed you could do a header only request. Will need a code example, let me dig you one out...

取决于你的服务器将返回什么,如果你可以得到标题,你只需要一个标准404页面然后你可以检查内容长度,以确保它不够大的图像,这听起来有点hacky但会工作(后来更好的方式)。类似于:

Depends on what your server will return, if you can get the headers and you just get a standard 404 page then you can check the content-length to ensure its not big enough for an image, that sounds a bit hacky but would work (theres a better way afterwards). Something like:

(从 http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html#M000682 )。

response = nil
Net::HTTP.start('www.example.com', 80) {|http|
  response = http.head('/myimage.html')
}
# Assume anything with a content-length greater than 1000 must be an image? 
# You will need to tweek/tune this to your server and your definition of what a good image is
p "Good image" unless response['content-length'] < 1000

或者您可以(并且应该以正确的方式执行)获取HTTP状态消息这就是服务器告诉你图像是否存在的明确方式。唯一的麻烦是你可能不得不下载整个东西,因为我不知道如何快速获取HTTP状态而不执行整个请求(请参阅上面链接的doc中的请求方法以获取详细信息)。

Alternatively you can (and should really to do it the right way) get the HTTP status message as thats the definitive way of the server telling you if the image is there or not. Only trouble is you might have to download the whole thing as I don't know of a quick way of getting just the HTTP status without doing a whole request (see request method in the doc linked above for details).

希望有所帮助。

这篇关于用自定义图像替换破碎的外部图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆