快速获取远程图像尺寸的方法
我使用 imagesize gem 来检查远程图像的大小,然后仅将足够大的图像推送到数组中。
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
我尝试在前端使用 JS 来检查图像尺寸,但浏览器似乎对一次可以加载的图像数量有限制。
使用 imagesize 比使用 JS 慢得多。有更好更快的方法来做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这个 gem 可以满足您的需求 https://github.com/sdsykes/fastimage
I think this gem does what you want https://github.com/sdsykes/fastimage