快速获取远程图像尺寸的方法 [英] Fast way to get remote image dimensions

查看:145
本文介绍了快速获取远程图像尺寸的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用imagesize gem来检查远程图像的大小,然后只将足够大的图像推送到数组中。

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

我尝试在前端使用JS检查图像尺寸,但似乎浏览器限制一次可以加载多少图像。

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.

使用imagesize执行它比使用JS要慢得多。有没有更好更快的方法呢?

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

推荐答案

我认为这个宝石做你想做的事情 https://github.com/sdsykes/fastimage

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


FastImage通过根据需要获取
来查找给定其uri的
图像的大小或类型

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

这篇关于快速获取远程图像尺寸的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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