Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast way to get remote image dimensions

Tags:

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?

like image 802
Aen Tan Avatar asked May 08 '11 11:05

Aen Tan


People also ask

How do I find out image URL size?

Hover over the URL to see the pop up with all of the image properties. This pop up includes the Rendered size – the dimensions that the website needs – as well as the Intrinsic size – the dimensions of the originally image uploaded.

How do I get the height and width of an image in react native?

React native Image component provides a method named as getSize which is used to get remote image dimensions in width and height. getSize() function of Image Component In React Native : Retrieve the width and height (in pixels) of an image prior to displaying it.

How do you get the image width and height using JS?

You can get the original width and height of an image using the HTML5 image naturalWidth and naturalHeight properties, which are supported in almost all major browsers. To get the current width and height, you can use the JavaScript clientWidth and clientHeight properties.


1 Answers

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

like image 112
Luqman Avatar answered Jan 05 '23 00:01

Luqman