Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

ruby on rails - Fast way to get remote image dimensions

I'm using the imagesize gem to check the sizes of remote images and then only push images that are big enough into an array.

require 'open-uri'
require 'image_size'
data = Nokogiri::HTML(open(url))
images = []
forcenocache = Time.now.to_i # No cache because jquery load event doesn't fire for cached images
data.css("img").each do |image|
  image_path = URI.join(site, URI.encode(image[:src]))
  open(image_path, "rb") do |fh|
    image_size = ImageSize.new(fh.read).get_size()
    unless image_size[0] < 200 || image_size[1] < 100
      image_element = "<img src="#{image_path}?#{forcenocache}">"
      images.push(image_element)
    end
  end
end

I tried using JS on the front-end to check image dimensions but there seems to be a browser limit to how many images can be loaded at once.

Doing it with imagesize is much slower than using JS. Any better and faster ways to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this gem does what you want https://github.com/sdsykes/fastimage

FastImage finds the size or type of an image given its uri by fetching as little as needed


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...