Skip to content

Commit

Permalink
document experimental uri reader and add changelog entries, resolves #17
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Sep 13, 2022
1 parent 1e9a4c6 commit dc2c619
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
39 changes: 38 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit dc2c619

Please sign in to comment.