diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 4c7a647..f297806 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -2,6 +2,8 @@ ## unreleased +* Improve experimental fetching of image meta from http server by reading only required amount of data when server does not support range header [@toy](https://github.com/toy) + ## v3.0.2 (2022-05-19) * Fix handling empty files [#20](https://github.com/toy/image_size/issues/20) [@toy](https://github.com/toy) @@ -15,6 +17,7 @@ * Read only required chunks of data for files and seekable IOs [@toy](https://github.com/toy) * Raise `FormatError` whenever reading data returns less data than expected [#12](https://github.com/toy/image_size/issues/12) [@toy](https://github.com/toy) * Add `w`/`width` and `h`/`height` accessors to `Size` [@toy](https://github.com/toy) +* Experimental efficient fetching of image meta from http server supporting range [@toy](https://github.com/toy) ## v2.1.2 (2021-08-21) diff --git a/README.markdown b/README.markdown index d7cdf04..f5f4c24 100644 --- a/README.markdown +++ b/README.markdown @@ -54,7 +54,7 @@ require 'image_size' image_size = ImageSize.new(ARGF) ``` -Works with `open-uri` if needed: +Works with `open-uri`, see [experimental interface below](#experimental-fetch-image-meta-from-http-server): ```ruby require 'image_size' @@ -90,6 +90,43 @@ File.open('spec/images/jpeg/436x429.jpeg', 'rb') do |fh| end ``` +### Experimental: fetch image meta from HTTP server + +If server recognises Range header, only needed chunks will be fetched even for TIFF images, otherwise required amount +of data will be fetched, in most cases first few kilobytes (TIFF images is an exception). + +```ruby +require 'image_size' +require 'image_size/uri_reader' + +url = 'http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg' +p ImageSize.url(url).size +``` + +This interface is as fast as dedicated gem fastimage for images with meta information in the header: + +```ruby +irb> url = 'http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg' +irb> puts Benchmark.measure{ p FastImage.size(url) } +[9545, 6623] + 0.004176 0.001974 0.006150 ( 0.282889) +irb> puts Benchmark.measure{ p ImageSize.url(url).size } +[9545, 6623] + 0.005604 0.001406 0.007010 ( 0.238629) +``` + +And considerably faster for images with meta information at the end of file: + +```ruby +irb> url = "https://upload.wikimedia.org/wikipedia/commons/c/c7/Curiosity%27s_Vehicle_System_Test_Bed_%28VSTB%29_Rover_%28PIA15876%29.tif" +irb> puts Benchmark.measure{ p FastImage.size(url) } +[7360, 4912] + 0.331284 0.247295 0.578579 ( 6.027051) +irb> puts Benchmark.measure{ p ImageSize.url(url).size } +[7360, 4912] + 0.006247 0.001045 0.007292 ( 0.197631) +``` + ## Licence This code is free to use under the terms of the [Ruby's licence](LICENSE.txt).