Fast way to get remote image dimensions

落花浮王杯 提交于 2019-11-27 11:48:20

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/5927252/fast-way-to-get-remote-image-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!