fastimage 2.2.7 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0365d74877c5c9bb640ef13fb701665b8fc511027ceba510be2cec62de03584c
4
- data.tar.gz: d15af188ae269215bce75d21d1f796680bd66a488367ab9a935919b50fe10a68
3
+ metadata.gz: e4eedb99b6314f24fb47381046bf63595b90b8eb8045a6b9b238a92acebc00ba
4
+ data.tar.gz: 10db6080223fb596a5fcfc904563a5f25b8209af1c4166260831e7aef9d7613e
5
5
  SHA512:
6
- metadata.gz: e9b26747ff490133917388f29203c1d8272dfe88c28b9a5208a3df1a55728779c3cfe203784c6df9d1bca91bcf58df0cf64e60220a468a0c9f6ca4f47963b3e0
7
- data.tar.gz: aa8f0a007f6cb710cbcea70241a56b9fd98fd54bb1a553be7c5533fca7b125b7df99ffbf5dcf5e996d87f60eb8a83933fd49aba848fc4a93c85ac540c8ae1577
6
+ metadata.gz: eae9ad37f27b853c2927183802b015ca6a066e413a112d80bb119603944d07d9fba5aac36712bc12c12acb327e3031a6b8c6875120e6f8de501e0526d4e2be14
7
+ data.tar.gz: 2b8e2add3af30fe7e856b84e18ba6f9425fb939e5e789f1601fb360abb91ee5197b15adaa6cff79a2bdb89ea7a9d95306ebc0513c4882cd0a30b7ab3261173d2
data/README.md ADDED
@@ -0,0 +1,201 @@
1
+ [![https://rubygems.org/gems/fastimage](https://img.shields.io/gem/dt/fastimage.svg)](https://rubygems.org/gems/fastimage)
2
+ [![https://github.com/sdsykes/fastimage/actions/workflows/test.yml](https://github.com/sdsykes/fastimage/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/sdsykes/fastimage/actions/workflows/test.yml)
3
+
4
+ # FastImage
5
+
6
+ **FastImage finds the size or type of an image given its uri by fetching as little as needed**
7
+
8
+ ## The problem
9
+
10
+ Your app needs to find the size or type of an image. This could be for adding width and height attributes to an image tag, for adjusting layouts or overlays to fit an image or any other of dozens of reasons.
11
+
12
+ But the image is not locally stored - it's on another asset server, or in the cloud - at Amazon S3 for example.
13
+
14
+ You don't want to download the entire image to your app server - it could be many tens of kilobytes, or even megabytes just to get this information. For most common image types (GIF, PNG, BMP etc.), the size of the image is simply stored at the start of the file. For JPEG files it's a little bit more complex, but even so you do not need to fetch much of the image to find the size.
15
+
16
+ FastImage does this minimal fetch for image types GIF, JPEG, PNG, TIFF, BMP, ICO, CUR, PSD, SVG and WEBP. And it doesn't rely on installing external libraries such as RMagick (which relies on ImageMagick or GraphicsMagick) or ImageScience (which relies on FreeImage).
17
+
18
+ You only need supply the uri, and FastImage will do the rest.
19
+
20
+ ## Features
21
+
22
+ - Reads local (and other) files - anything that is not parseable as a URI will be interpreted as a filename, and - will attempt to open it with `File#open`.
23
+
24
+ - Automatically reads from any object that responds to `:read` - for instance an IO object if that is passed instead of a URI.
25
+
26
+ - Follows up to 4 HTTP redirects to get the image.
27
+
28
+ - Obey the `http_proxy` setting in your environment to route requests via a proxy. You can also pass a `:proxy` argument if you want to specify the proxy address in the call.
29
+
30
+ - Add a timeout to the request which will limit the request time by passing `:timeout => number_of_seconds`.
31
+
32
+ - Returns `nil` if it encounters an error, but you can pass `:raise_on_failure => true` to get an exception.
33
+
34
+ - Provides a reader for the content length header provided in HTTP. This may be useful to assess the file size of an image, but do not rely on it exclusively - it will not be present in chunked responses for instance.
35
+
36
+ - Accepts additional HTTP headers. This can be used to set a user agent or referrer which some servers require. Pass an `:http_header` argument to specify headers, e.g., `:http_header => {'User-Agent' => 'Fake Browser'}`.
37
+
38
+ - Gives you information about the parsed display orientation of an image with Exif data (jpeg or tiff).
39
+
40
+ - Handles [Data URIs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs) correctly.
41
+
42
+ ## Security
43
+
44
+ Take care to sanitise the strings passed to FastImage; it will try to read from whatever is passed.
45
+
46
+ ## Examples
47
+
48
+ ```ruby
49
+ require 'fastimage'
50
+
51
+ FastImage.size("https://switchstep.com/images/ios.gif")
52
+ => [196, 283] # width, height
53
+ FastImage.type("http://switchstep.com/images/ss_logo.png")
54
+ => :png
55
+ FastImage.type("/some/local/file.gif")
56
+ => :gif
57
+ File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
58
+ => :gif
59
+ FastImage.size("http://switchstep.com/images/favicon.ico", :raise_on_failure=>true, :timeout=>0.01)
60
+ => raises FastImage::ImageFetchFailure
61
+ FastImage.size("http://switchstep.com/images/favicon.ico", :raise_on_failure=>true, :timeout=>2)
62
+ => [16, 16]
63
+ FastImage.size("http://switchstep.com/images/faulty.jpg", :raise_on_failure=>true)
64
+ => raises FastImage::SizeNotFound
65
+ FastImage.new("http://switchstep.com/images/ss_logo.png").content_length
66
+ => 4679
67
+ FastImage.size("http://switchstep.com/images/ss_logo.png", :http_header => {'User-Agent' => 'Fake Browser'})
68
+ => [300, 300]
69
+ FastImage.new("http://switchstep.com/images/ExifOrientation3.jpg").orientation
70
+ => 3
71
+ FastImage.size("data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
72
+ => [1, 1]
73
+ ```
74
+
75
+ ## Installation
76
+
77
+ ### Gem
78
+
79
+ ```sh
80
+ gem install fastimage
81
+ ```
82
+
83
+ ### Bundler
84
+
85
+ Add fastimage to your Gemfile.
86
+
87
+ ```ruby
88
+ gem 'fastimage'
89
+ ```
90
+
91
+ Then you're off - just use `FastImage.size()` and `FastImage.type()` in your code as in the examples.
92
+
93
+ ## Documentation
94
+
95
+ http://sdsykes.github.io/fastimage/rdoc/FastImage.html
96
+
97
+ ## Maintainer
98
+
99
+ FastImage is maintained by Stephen Sykes (sdsykes). SamSaffron also helps out from time to time (thanks!).
100
+
101
+ ## Benchmark
102
+
103
+ It's way faster than conventional methods for most types of file when fetching over the wire. Compared here by using OpenURI which will fetch the whole file.
104
+
105
+ ```ruby
106
+ require 'benchmark'
107
+ require 'fastimage'
108
+ require 'open-uri'
109
+
110
+ uri = "http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg"
111
+ puts Benchmark.measure {URI.open(uri, 'rb') {|fh| p FastImage.size(fh)}}
112
+ [9545, 6623]
113
+ 0.059088 0.067694 0.126782 ( 0.808131)
114
+
115
+ puts Benchmark.measure {p FastImage.size(uri)}
116
+ [9545, 6623]
117
+ 0.006198 0.001563 0.007761 ( 0.162021)
118
+ ```
119
+
120
+ The file is fetched in about 0.8 seconds in this test (the number in brackets is the total time taken), but as FastImage doesn't need to fetch the whole thing, it completes in 0.16s.
121
+
122
+ You'll see similar excellent results for the other file types.
123
+
124
+ ```ruby
125
+ require 'benchmark'
126
+ require 'fastimage'
127
+ require 'open-uri'
128
+
129
+ uri = "https://upload.wikimedia.org/wikipedia/commons/a/a9/Augustine_Herrman_1670_Map_Virginia_Maryland.tiff"
130
+ puts Benchmark.measure {URI.open(uri, 'rb') {|fh| p FastImage.size(fh)}}
131
+ [12805, 10204]
132
+ 1.332587 2.049915 3.382502 ( 19.925270)
133
+
134
+ puts Benchmark.measure {p FastImage.size(uri)}
135
+ [12805, 10204]
136
+ 0.004593 0.000924 0.005517 ( 0.100592)
137
+ ```
138
+
139
+ Some tiff files however do not have their metadata near the start of the file.
140
+
141
+ ```ruby
142
+ require 'benchmark'
143
+ require 'fastimage'
144
+ require 'open-uri'
145
+
146
+ uri = "https://upload.wikimedia.org/wikipedia/commons/1/14/Center-Filled_LIMA.tif"
147
+ puts Benchmark.measure {URI.open(uri, 'rb') {|fh| p FastImage.size(fh)}}
148
+ [22841, 19404]
149
+ 0.350304 0.321104 0.671408 ( 3.053605)
150
+
151
+ puts Benchmark.measure {p FastImage.size(uri)}
152
+ [22841, 19404]
153
+ 0.163443 0.214301 0.377744 ( 2.880414)
154
+ ```
155
+
156
+ Note that if you want a really fast result for this file type, [image_size](https://github.com/toy/image_size?tab=readme-ov-file#experimental-fetch-image-meta-from-http-server) might be useful as it has an optimisation for this (fetching only the needed data range).
157
+
158
+ ```ruby
159
+ require 'benchmark'
160
+ require 'image_size/uri'
161
+ require 'fastimage'
162
+
163
+ uri = "https://upload.wikimedia.org/wikipedia/commons/1/14/Center-Filled_LIMA.tif"
164
+ puts Benchmark.measure {p ImageSize.url(uri).size }
165
+ [22841, 19404]
166
+ 0.008983 0.001311 0.010294 ( 0.128986)
167
+
168
+ puts Benchmark.measure {p FastImage.size(uri)}
169
+ [22841, 19404]
170
+ 0.163443 0.214301 0.377744 ( 2.880414)
171
+ ```
172
+
173
+ ## Tests
174
+
175
+ You'll need to bundle, or `gem install fakeweb` and possibly also `gem install test-unit` to be able to run the tests.
176
+
177
+ ```sh
178
+ ruby test/test.rb
179
+ ```
180
+
181
+ ## References
182
+
183
+ - [Pennysmalls - Find jpeg dimensions fast in pure Ruby, no image library needed](http://pennysmalls.wordpress.com/2008/08/19/find-jpeg-dimensions-fast-in-pure-ruby-no-ima/)
184
+ - [Antti Kupila - Getting JPG dimensions with AS3 without loading the entire file](https://www.anttikupila.com/archive/getting-jpg-dimensions/)
185
+ - [imagesize gem](https://rubygems.org/gems/imagesize)
186
+ - [EXIF Reader](https://github.com/remvee/exifr)
187
+
188
+ ## FastImage in other languages
189
+
190
+ - [Python by bmuller](https://github.com/bmuller/fastimage)
191
+ - [Swift by kaishin](https://github.com/kaishin/ImageScout)
192
+ - [Go by rubenfonseca](https://github.com/rubenfonseca/fastimage)
193
+ - [PHP by tommoor](https://github.com/tommoor/fastimage)
194
+ - [Node.js by ShogunPanda](https://github.com/ShogunPanda/fastimage)
195
+ - [Objective C by kylehickinson](https://github.com/kylehickinson/FastImage)
196
+ - [Android by qstumn](https://github.com/qstumn/FastImageSize)
197
+ - [Flutter by ky1vstar](https://github.com/ky1vstar/fastimage.dart)
198
+
199
+ ## Licence
200
+
201
+ MIT, see file "MIT-LICENSE"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class FastImage
4
- VERSION = '2.2.7'
4
+ VERSION = '2.3.1'
5
5
  end
data/lib/fastimage.rb CHANGED
@@ -35,17 +35,17 @@
35
35
  # === Examples
36
36
  # require 'fastimage'
37
37
  #
38
- # FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
39
- # => [266, 56]
40
- # FastImage.type("http://stephensykes.com/images/pngimage")
38
+ # FastImage.size("https://switchstep.com/images/ios.gif")
39
+ # => [196, 283]
40
+ # FastImage.type("http://switchstep.com/images/ss_logo.png")
41
41
  # => :png
42
42
  # FastImage.type("/some/local/file.gif")
43
43
  # => :gif
44
44
  # File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
45
45
  # => :gif
46
- # FastImage.new("http://stephensykes.com/images/pngimage").content_length
47
- # => 432
48
- # FastImage.new("http://stephensykes.com/images/ExifOrientation3.jpg").orientation
46
+ # FastImage.new("http://switchstep.com/images/ss_logo.png").content_length
47
+ # => 4679
48
+ # FastImage.new("http://switchstep.com/images/ExifOrientation3.jpg").orientation
49
49
  # => 3
50
50
  #
51
51
  # === References
@@ -61,6 +61,7 @@ require 'pathname'
61
61
  require 'zlib'
62
62
  require 'base64'
63
63
  require 'uri'
64
+ require 'stringio'
64
65
  require_relative 'fastimage/version'
65
66
 
66
67
  # see http://stackoverflow.com/questions/5208851/i/41048816#41048816
@@ -109,27 +110,27 @@ class FastImage
109
110
  #
110
111
  # require 'fastimage'
111
112
  #
112
- # FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
113
- # => [266, 56]
114
- # FastImage.size("http://stephensykes.com/images/pngimage")
115
- # => [16, 16]
116
- # FastImage.size("http://farm4.static.flickr.com/3023/3047236863_9dce98b836.jpg")
117
- # => [500, 375]
118
- # FastImage.size("http://www-ece.rice.edu/~wakin/images/lena512.bmp")
113
+ # FastImage.size("https://switchstep.com/images/ios.gif")
114
+ # => [196, 283]
115
+ # FastImage.size("http://switchstep.com/images/ss_logo.png")
116
+ # => [300, 300]
117
+ # FastImage.size("https://upload.wikimedia.org/wikipedia/commons/0/09/Jpeg_thumb_artifacts_test.jpg")
118
+ # => [1280, 800]
119
+ # FastImage.size("https://eeweb.engineering.nyu.edu/~yao/EL5123/image/lena_gray.bmp")
119
120
  # => [512, 512]
120
121
  # FastImage.size("test/fixtures/test.jpg")
121
122
  # => [882, 470]
122
- # FastImage.size("http://pennysmalls.com/does_not_exist")
123
+ # FastImage.size("http://switchstep.com/does_not_exist")
123
124
  # => nil
124
- # FastImage.size("http://pennysmalls.com/does_not_exist", :raise_on_failure=>true)
125
+ # FastImage.size("http://switchstep.com/does_not_exist", :raise_on_failure=>true)
125
126
  # => raises FastImage::ImageFetchFailure
126
- # FastImage.size("http://stephensykes.com/favicon.ico", :raise_on_failure=>true)
127
+ # FastImage.size("http://switchstep.com/images/favicon.ico", :raise_on_failure=>true)
127
128
  # => [16, 16]
128
- # FastImage.size("http://stephensykes.com/images/squareBlue.icns", :raise_on_failure=>true)
129
+ # FastImage.size("http://switchstep.com/foo.ics", :raise_on_failure=>true)
129
130
  # => raises FastImage::UnknownImageType
130
- # FastImage.size("http://stephensykes.com/favicon.ico", :raise_on_failure=>true, :timeout=>0.01)
131
+ # FastImage.size("http://switchstep.com/images/favicon.ico", :raise_on_failure=>true, :timeout=>0.01)
131
132
  # => raises FastImage::ImageFetchFailure
132
- # FastImage.size("http://stephensykes.com/images/faulty.jpg", :raise_on_failure=>true)
133
+ # FastImage.size("http://switchstep.com/images/faulty.jpg", :raise_on_failure=>true)
133
134
  # => raises FastImage::SizeNotFound
134
135
  #
135
136
  # === Supported options
@@ -155,17 +156,17 @@ class FastImage
155
156
  #
156
157
  # require 'fastimage'
157
158
  #
158
- # FastImage.type("http://stephensykes.com/images/ss.com_x.gif")
159
+ # FastImage.type("https://switchstep.com/images/ios.gif")
159
160
  # => :gif
160
- # FastImage.type("http://stephensykes.com/images/pngimage")
161
+ # FastImage.type("http://switchstep.com/images/ss_logo.png")
161
162
  # => :png
162
- # FastImage.type("http://farm4.static.flickr.com/3023/3047236863_9dce98b836.jpg")
163
+ # FastImage.type("https://upload.wikimedia.org/wikipedia/commons/0/09/Jpeg_thumb_artifacts_test.jpg")
163
164
  # => :jpeg
164
- # FastImage.type("http://www-ece.rice.edu/~wakin/images/lena512.bmp")
165
+ # FastImage.type("https://eeweb.engineering.nyu.edu/~yao/EL5123/image/lena_gray.bmp")
165
166
  # => :bmp
166
167
  # FastImage.type("test/fixtures/test.jpg")
167
168
  # => :jpeg
168
- # FastImage.type("http://stephensykes.com/does_not_exist")
169
+ # FastImage.type("http://switchstep.com/does_not_exist")
169
170
  # => nil
170
171
  # File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
171
172
  # => :gif
@@ -320,6 +321,7 @@ class FastImage
320
321
  res.read_body do |str|
321
322
  Fiber.yield str
322
323
  end
324
+ nil
323
325
  end
324
326
 
325
327
  case res['content-encoding']
@@ -334,6 +336,7 @@ class FastImage
334
336
  while data = gzip.readline
335
337
  Fiber.yield data
336
338
  end
339
+ nil
337
340
  end
338
341
  end
339
342
 
@@ -387,12 +390,14 @@ class FastImage
387
390
  Fiber.yield str
388
391
  offset += LocalFileChunkSize
389
392
  end
393
+ nil
390
394
  end
391
395
  else
392
396
  read_fiber = Fiber.new do
393
397
  while str = readable.read(LocalFileChunkSize)
394
398
  Fiber.yield str
395
399
  end
400
+ nil
396
401
  end
397
402
  end
398
403
 
@@ -435,7 +440,7 @@ class FastImage
435
440
 
436
441
  def parse_animated
437
442
  @type = parse_type unless @type
438
- %i(gif webp avif).include?(@type) ? send("parse_animated_for_#{@type}") : nil
443
+ %i(gif png webp avif).include?(@type) ? send("parse_animated_for_#{@type}") : nil
439
444
  end
440
445
 
441
446
  def fetch_using_base64(uri)
@@ -470,6 +475,9 @@ class FastImage
470
475
  include StreamUtil
471
476
  attr_reader :pos
472
477
 
478
+ # read_fiber should return nil if it no longer has anything to return when resumed
479
+ # so the result of the whole Fiber block should be set to be nil in case yield is no
480
+ # longer called
473
481
  def initialize(read_fiber)
474
482
  @read_fiber = read_fiber
475
483
  @pos = 0
@@ -483,7 +491,6 @@ class FastImage
483
491
  unused_str = @str[@strpos..-1]
484
492
 
485
493
  new_string = @read_fiber.resume
486
- new_string = @read_fiber.resume if new_string.is_a? Net::ReadAdapter
487
494
  raise CannotParseImage if !new_string
488
495
  # we are dealing with bytes here, so force the encoding
489
496
  new_string.force_encoding("ASCII-8BIT") if new_string.respond_to? :force_encoding
@@ -569,10 +576,10 @@ class FastImage
569
576
  when /\s\s|\s<|<[?!]/, 0xef.chr + 0xbb.chr
570
577
  # Peek 10 more chars each time, and if end of file is reached just raise
571
578
  # unknown. We assume the <svg tag cannot be within 10 chars of the end of
572
- # the file, and is within the first 250 chars.
579
+ # the file, and is within the first 1000 chars.
573
580
  begin
574
- :svg if (1..25).detect {|n| @stream.peek(10 * n).include?("<svg")}
575
- rescue FiberError
581
+ :svg if (1..100).detect {|n| @stream.peek(10 * n).include?("<svg")}
582
+ rescue FiberError, CannotParseImage
576
583
  nil
577
584
  end
578
585
  end
@@ -649,9 +656,9 @@ class FastImage
649
656
  when "ispe"
650
657
  handle_ispe_box(box_size, index)
651
658
  when "mdat"
652
- throw :finish
659
+ @stream.skip(box_size)
653
660
  else
654
- @stream.read(box_size)
661
+ @stream.skip(box_size)
655
662
  end
656
663
 
657
664
  index += 1
@@ -729,6 +736,7 @@ class FastImage
729
736
  def read_box_header!
730
737
  size = read_uint32!
731
738
  type = @stream.read(4)
739
+ size = read_uint64! - 8 if size == 1
732
740
  [type, size - 8]
733
741
  end
734
742
 
@@ -743,6 +751,10 @@ class FastImage
743
751
  def read_uint32!
744
752
  @stream.read(4).unpack("N")[0]
745
753
  end
754
+
755
+ def read_uint64!
756
+ @stream.read(8).unpack("Q>")[0]
757
+ end
746
758
  end
747
759
 
748
760
  def parse_size_for_avif
@@ -1095,6 +1107,25 @@ class FastImage
1095
1107
  gif.animated?
1096
1108
  end
1097
1109
 
1110
+ def parse_animated_for_png
1111
+ # Signature (8) + IHDR chunk (4 + 4 + 13 + 4)
1112
+ @stream.read(33)
1113
+
1114
+ loop do
1115
+ length = @stream.read(4).unpack("L>")[0]
1116
+ type = @stream.read(4)
1117
+
1118
+ case type
1119
+ when "acTL"
1120
+ return true
1121
+ when "IDAT"
1122
+ return false
1123
+ end
1124
+
1125
+ @stream.skip(length + 4)
1126
+ end
1127
+ end
1128
+
1098
1129
  def parse_animated_for_webp
1099
1130
  vp8 = @stream.read(16)[12..15]
1100
1131
  _len = @stream.read(4).unpack("V")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastimage
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.7
4
+ version: 2.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Sykes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-28 00:00:00.000000000 Z
11
+ date: 2024-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fakeweb-fi
@@ -72,10 +72,10 @@ email: sdsykes@gmail.com
72
72
  executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files:
75
- - README.textile
75
+ - README.md
76
76
  files:
77
77
  - MIT-LICENSE
78
- - README.textile
78
+ - README.md
79
79
  - lib/fastimage.rb
80
80
  - lib/fastimage/version.rb
81
81
  homepage: http://github.com/sdsykes/fastimage
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  requirements: []
101
- rubygems_version: 3.4.6
101
+ rubygems_version: 3.4.10
102
102
  signing_key:
103
103
  specification_version: 4
104
104
  summary: FastImage - Image info fast
data/README.textile DELETED
@@ -1,163 +0,0 @@
1
- !https://img.shields.io/gem/dt/fastimage.svg!:https://rubygems.org/gems/fastimage
2
- !https://github.com/sdsykes/fastimage/actions/workflows/test.yml/badge.svg?branch=master!:https://github.com/sdsykes/fastimage/actions/workflows/test.yml
3
-
4
- h1. FastImage
5
-
6
- h4. FastImage finds the size or type of an image given its uri by fetching as little as needed
7
-
8
- h2. The problem
9
-
10
- Your app needs to find the size or type of an image. This could be for adding width and height attributes to an image tag, for adjusting layouts or overlays to fit an image or any other of dozens of reasons.
11
-
12
- But the image is not locally stored - it's on another asset server, or in the cloud - at Amazon S3 for example.
13
-
14
- You don't want to download the entire image to your app server - it could be many tens of kilobytes, or even megabytes just to get this information. For most common image types (GIF, PNG, BMP etc.), the size of the image is simply stored at the start of the file. For JPEG files it's a little bit more complex, but even so you do not need to fetch much of the image to find the size.
15
-
16
- FastImage does this minimal fetch for image types GIF, JPEG, PNG, TIFF, BMP, ICO, CUR, PSD, SVG and WEBP. And it doesn't rely on installing external libraries such as RMagick (which relies on ImageMagick or GraphicsMagick) or ImageScience (which relies on FreeImage).
17
-
18
- You only need supply the uri, and FastImage will do the rest.
19
-
20
- h2. Features
21
-
22
- FastImage can also read local (and other) files - anything that is not parseable as a URI will be interpreted as a filename, and FastImage will attempt to open it with @File#open@.
23
-
24
- FastImage will also automatically read from any object that responds to @:read@ - for instance an IO object if that is passed instead of a URI.
25
-
26
- FastImage will follow up to 4 HTTP redirects to get the image.
27
-
28
- FastImage will obey the @http_proxy@ setting in your environment to route requests via a proxy. You can also pass a @:proxy@ argument if you want to specify the proxy address in the call.
29
-
30
- You can add a timeout to the request which will limit the request time by passing @:timeout => number_of_seconds@.
31
-
32
- FastImage normally replies with @nil@ if it encounters an error, but you can pass @:raise_on_failure => true@ to get an exception.
33
-
34
- FastImage also provides a reader for the content length header provided in HTTP. This may be useful to assess the file size of an image, but do not rely on it exclusively - it will not be present in chunked responses for instance.
35
-
36
- FastImage accepts additional HTTP headers. This can be used to set a user agent or referrer which some servers require. Pass an @:http_header@ argument to specify headers, e.g., @:http_header => {'User-Agent' => 'Fake Browser'}@.
37
-
38
- FastImage can give you information about the parsed display orientation of an image with Exif data (jpeg or tiff).
39
-
40
- FastImage also handles "Data URIs":https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs correctly.
41
-
42
- h2. Security
43
-
44
- As of v1.6.7 FastImage no longer uses @openuri@ to open files, but directly calls @File.open@. Take care to sanitise the strings passed to FastImage; it will try to read from whatever is passed.
45
-
46
- h2. Examples
47
-
48
- <pre lang="ruby"><code>
49
- require 'fastimage'
50
-
51
- FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
52
- => [266, 56] # width, height
53
- FastImage.type("http://stephensykes.com/images/pngimage")
54
- => :png
55
- FastImage.type("/some/local/file.gif")
56
- => :gif
57
- FastImage.size("http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg", :raise_on_failure=>true, :timeout=>0.1)
58
- => FastImage::ImageFetchFailure: FastImage::ImageFetchFailure
59
- FastImage.size("http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg", :raise_on_failure=>true, :timeout=>2.0)
60
- => [9545, 6623]
61
- FastImage.new("http://stephensykes.com/images/pngimage").content_length
62
- => 432
63
- FastImage.size("http://stephensykes.com/images/ss.com_x.gif", :http_header => {'User-Agent' => 'Fake Browser'})
64
- => [266, 56]
65
- FastImage.new("http://stephensykes.com/images/ExifOrientation3.jpg").orientation
66
- => 3
67
- FastImage.size("data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
68
- => [1, 1]
69
- </code></pre>
70
-
71
- h2. Installation
72
-
73
- h4. Required Ruby version
74
-
75
- FastImage version 2.0.0 and above work with Ruby 1.9.2 and above.
76
-
77
- FastImage version 1.9.0 was the last version that supported Ruby 1.8.7.
78
-
79
- h4. Gem
80
-
81
- bc. gem install fastimage
82
-
83
- h4. Rails
84
-
85
- Add fastimage to your Gemfile, and bundle.
86
-
87
- bc. gem 'fastimage'
88
-
89
- Then you're off - just use @FastImage.size()@ and @FastImage.type()@ in your code as in the examples.
90
-
91
- h2. Documentation
92
-
93
- "http://sdsykes.github.io/fastimage/rdoc/FastImage.html":http://sdsykes.github.io/fastimage/rdoc/FastImage.html
94
-
95
- h2. Maintainer
96
-
97
- FastImage is maintained by Stephen Sykes (@sdsykes). Support this project by using "LibPixel":https://libpixel.com cloud based image resizing and processing service.
98
-
99
- h2. Benchmark
100
-
101
- It's way faster than conventional methods (for example the image_size gem) for most types of file when fetching over the wire.
102
-
103
- <pre lang="ruby"><code>
104
- irb> uri = "http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg"
105
- irb> puts Benchmark.measure {open(uri, 'rb') {|fh| p ImageSize.new(fh).size}}
106
- [9545, 6623]
107
- 0.680000 0.250000 0.930000 ( 7.571887)
108
-
109
- irb> puts Benchmark.measure {p FastImage.size(uri)}
110
- [9545, 6623]
111
- 0.010000 0.000000 0.010000 ( 0.090640)
112
- </code></pre>
113
-
114
- The file is fetched in about 7.5 seconds in this test (the number in brackets is the total time taken), but as FastImage doesn't need to fetch the whole thing, it completes in less than 0.1s.
115
-
116
- You'll see similar excellent results for the other file types, except for TIFF. Unfortunately TIFFs tend to have their
117
- metadata towards the end of the file, so it makes little difference to do a minimal fetch. The result shown below is
118
- mostly dependent on the exact internet conditions during the test, and little to do with the library used.
119
-
120
- <pre lang="ruby"><code>
121
- irb> uri = "http://upload.wikimedia.org/wikipedia/commons/1/11/Shinbutsureijoushuincho.tiff"
122
- irb> puts Benchmark.measure {open(uri, 'rb') {|fh| p ImageSize.new(fh).size}}
123
- [1120, 1559]
124
- 1.080000 0.370000 1.450000 ( 13.766962)
125
-
126
- irb> puts Benchmark.measure {p FastImage.size(uri)}
127
- [1120, 1559]
128
- 3.490000 3.810000 7.300000 ( 11.754315)
129
- </code></pre>
130
-
131
- h2. Tests
132
-
133
- You'll need to @gem install fakeweb@ and possibly also @gem install test-unit@ to be able to run the tests.
134
-
135
- bc.. $ ruby test/test.rb
136
- Run options:
137
-
138
- # Running tests:
139
-
140
- Finished tests in 1.033640s, 23.2189 tests/s, 82.2337 assertions/s.
141
- 24 tests, 85 assertions, 0 failures, 0 errors, 0 skips
142
-
143
- h2. References
144
-
145
- * "Pennysmalls - Find jpeg dimensions fast in pure Ruby, no image library needed":http://pennysmalls.wordpress.com/2008/08/19/find-jpeg-dimensions-fast-in-pure-ruby-no-ima/
146
- * "Antti Kupila - Getting JPG dimensions with AS3 without loading the entire file":http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/
147
- * "imagesize gem":https://rubygems.org/gems/imagesize
148
- * "EXIF Reader":https://github.com/remvee/exifr
149
-
150
- h2. FastImage in other languages
151
-
152
- * "Python by bmuller":https://github.com/bmuller/fastimage
153
- * "Swift by kaishin":https://github.com/kaishin/ImageScout
154
- * "Go by rubenfonseca":https://github.com/rubenfonseca/fastimage
155
- * "PHP by tommoor":https://github.com/tommoor/fastimage
156
- * "Node.js by ShogunPanda":https://github.com/ShogunPanda/fastimage
157
- * "Objective C by kylehickinson":https://github.com/kylehickinson/FastImage
158
- * "Android by qstumn":https://github.com/qstumn/FastImageSize
159
- * "Flutter by ky1vstar":https://github.com/ky1vstar/fastimage.dart
160
-
161
- h2. Licence
162
-
163
- MIT, see file "MIT-LICENSE":MIT-LICENSE