How to check if an image exists on the web
Posted by Paolo Sat, 09 Jun 2007 10:32:00 GMT
require 'net/http'
require 'uri'
def exist?(image_url)
Timeout::timeout(5) do
url = URI.parse(image_url)
req = Net::HTTP::Head.new(url.path)
res = Net::HTTP.start(url.host, url.port) do |h|
h.request(req)
end
return res.message.strip.downcase == "ok"
end
rescue Exception
false
end
# usage:
exist? 'http://www.seesaw.it/images/logo.png' #=> true
exist? 'http://foo.com/bar.gif' #=> false






