fastimage 2.2.6 → 2.3.0

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: d7667683a4bbc8ac16d583fb3ed7c93e6a58a25c19bf8df909ab7dae8d4ef782
4
- data.tar.gz: 0baf5dcf6af104d0edd38baf65b7b35c972e6fc4271363d52855071e22b97a82
3
+ metadata.gz: fa0337307f5e98ae916e46768e148812f0f6dbb93a1c67d193ca981540779336
4
+ data.tar.gz: 8600c44df8eed5762ec526709d46e32841a099c0c453e7c255c2a87611dcb822
5
5
  SHA512:
6
- metadata.gz: 9261157ade925ba5be4761028b4d70a698f1cc4d54e4ef0fcb7f4b23b6120ae6f9c42516b0dc17f915c423d168a4ef66c805a686ea20e3ca311556ce3e930593
7
- data.tar.gz: 2d319a0aa6c4e7c7a3d951b3025d6215ac999bc2efe5d054720dac9c063cb3e2939e35e30660cf360bfae5f88285b91bb8029ec594c64ca1eb9e3e4144fa6b4c
6
+ metadata.gz: 57b4e77535ee1022ec9b1d3209536729dd5e980c2799fc6987d3c91731f5dd4dce3d93ef29f97c8762b6021b12795554efbc3ca1db7a49713aa119bdb5df5088
7
+ data.tar.gz: 83741942ce879586ae813fda60da760bee079ff1000dbbf7ee118b2513d579ae4cff8acf98de0bafe5313d429352053380e8d0293a101b0a3b1a799ce457f5a1
data/README.md ADDED
@@ -0,0 +1,161 @@
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). Support this project by using [LibPixel](https://libpixel.com) cloud based image resizing and processing service.
100
+
101
+ ## Benchmark
102
+
103
+ It's way faster than conventional methods (for example the image_size gem) for most types of file when fetching over the wire.
104
+
105
+ ```ruby
106
+ irb> uri = "http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg"
107
+ irb> puts Benchmark.measure {open(uri, 'rb') {|fh| p ImageSize.new(fh).size}}
108
+ [9545, 6623]
109
+ 0.680000 0.250000 0.930000 ( 7.571887)
110
+
111
+ irb> puts Benchmark.measure {p FastImage.size(uri)}
112
+ [9545, 6623]
113
+ 0.010000 0.000000 0.010000 ( 0.090640)
114
+ ```
115
+
116
+ 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.
117
+
118
+ You'll see similar excellent results for the other file types, except for TIFF. Unfortunately TIFFs tend to have their
119
+ metadata towards the end of the file, so it makes little difference to do a minimal fetch. The result shown below is
120
+ mostly dependent on the exact internet conditions during the test, and little to do with the library used.
121
+
122
+ ```ruby
123
+ irb> uri = "http://upload.wikimedia.org/wikipedia/commons/1/11/Shinbutsureijoushuincho.tiff"
124
+ irb> puts Benchmark.measure {open(uri, 'rb') {|fh| p ImageSize.new(fh).size}}
125
+ [1120, 1559]
126
+ 1.080000 0.370000 1.450000 ( 13.766962)
127
+
128
+ irb> puts Benchmark.measure {p FastImage.size(uri)}
129
+ [1120, 1559]
130
+ 3.490000 3.810000 7.300000 ( 11.754315)
131
+ ```
132
+
133
+ ## Tests
134
+
135
+ You'll need to bundle, or `gem install fakeweb` and possibly also `gem install test-unit` to be able to run the tests.
136
+
137
+ ```sh
138
+ ruby test/test.rb
139
+ ```
140
+
141
+ ## References
142
+
143
+ - [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/)
144
+ - [Antti Kupila - Getting JPG dimensions with AS3 without loading the entire file](https://www.anttikupila.com/archive/getting-jpg-dimensions/)
145
+ - [imagesize gem](https://rubygems.org/gems/imagesize)
146
+ - [EXIF Reader](https://github.com/remvee/exifr)
147
+
148
+ ## FastImage in other languages
149
+
150
+ - [Python by bmuller](https://github.com/bmuller/fastimage)
151
+ - [Swift by kaishin](https://github.com/kaishin/ImageScout)
152
+ - [Go by rubenfonseca](https://github.com/rubenfonseca/fastimage)
153
+ - [PHP by tommoor](https://github.com/tommoor/fastimage)
154
+ - [Node.js by ShogunPanda](https://github.com/ShogunPanda/fastimage)
155
+ - [Objective C by kylehickinson](https://github.com/kylehickinson/FastImage)
156
+ - [Android by qstumn](https://github.com/qstumn/FastImageSize)
157
+ - [Flutter by ky1vstar](https://github.com/ky1vstar/fastimage.dart)
158
+
159
+ ## Licence
160
+
161
+ 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.6'
4
+ VERSION = '2.3.0'
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
@@ -85,6 +86,8 @@ class FastImage
85
86
  end
86
87
  class CannotParseImage < FastImageException # :nodoc:
87
88
  end
89
+ class BadImageURI < FastImageException # :nodoc:
90
+ end
88
91
 
89
92
  DefaultTimeout = 2 unless const_defined?(:DefaultTimeout)
90
93
 
@@ -107,27 +110,27 @@ class FastImage
107
110
  #
108
111
  # require 'fastimage'
109
112
  #
110
- # FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
111
- # => [266, 56]
112
- # FastImage.size("http://stephensykes.com/images/pngimage")
113
- # => [16, 16]
114
- # FastImage.size("http://farm4.static.flickr.com/3023/3047236863_9dce98b836.jpg")
115
- # => [500, 375]
116
- # 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")
117
120
  # => [512, 512]
118
121
  # FastImage.size("test/fixtures/test.jpg")
119
122
  # => [882, 470]
120
- # FastImage.size("http://pennysmalls.com/does_not_exist")
123
+ # FastImage.size("http://switchstep.com/does_not_exist")
121
124
  # => nil
122
- # 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)
123
126
  # => raises FastImage::ImageFetchFailure
124
- # FastImage.size("http://stephensykes.com/favicon.ico", :raise_on_failure=>true)
127
+ # FastImage.size("http://switchstep.com/images/favicon.ico", :raise_on_failure=>true)
125
128
  # => [16, 16]
126
- # FastImage.size("http://stephensykes.com/images/squareBlue.icns", :raise_on_failure=>true)
129
+ # FastImage.size("http://switchstep.com/foo.ics", :raise_on_failure=>true)
127
130
  # => raises FastImage::UnknownImageType
128
- # 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)
129
132
  # => raises FastImage::ImageFetchFailure
130
- # 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)
131
134
  # => raises FastImage::SizeNotFound
132
135
  #
133
136
  # === Supported options
@@ -153,17 +156,17 @@ class FastImage
153
156
  #
154
157
  # require 'fastimage'
155
158
  #
156
- # FastImage.type("http://stephensykes.com/images/ss.com_x.gif")
159
+ # FastImage.type("https://switchstep.com/images/ios.gif")
157
160
  # => :gif
158
- # FastImage.type("http://stephensykes.com/images/pngimage")
161
+ # FastImage.type("http://switchstep.com/images/ss_logo.png")
159
162
  # => :png
160
- # 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")
161
164
  # => :jpeg
162
- # 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")
163
166
  # => :bmp
164
167
  # FastImage.type("test/fixtures/test.jpg")
165
168
  # => :jpeg
166
- # FastImage.type("http://stephensykes.com/does_not_exist")
169
+ # FastImage.type("http://switchstep.com/does_not_exist")
167
170
  # => nil
168
171
  # File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
169
172
  # => :gif
@@ -228,6 +231,8 @@ class FastImage
228
231
  :size
229
232
  end
230
233
 
234
+ raise BadImageURI if uri.nil?
235
+
231
236
  @type, @state = nil
232
237
 
233
238
  if uri.respond_to?(:read)
@@ -254,7 +259,7 @@ class FastImage
254
259
  Errno::ENETUNREACH, ImageFetchFailure, Net::HTTPBadResponse, EOFError, Errno::ENOENT,
255
260
  OpenSSL::SSL::SSLError
256
261
  raise ImageFetchFailure if @options[:raise_on_failure]
257
- rescue UnknownImageType
262
+ rescue UnknownImageType, BadImageURI
258
263
  raise if @options[:raise_on_failure]
259
264
  rescue CannotParseImage
260
265
  if @options[:raise_on_failure]
@@ -431,7 +436,7 @@ class FastImage
431
436
 
432
437
  def parse_animated
433
438
  @type = parse_type unless @type
434
- @type == :gif ? send("parse_animated_for_#{@type}") : nil
439
+ %i(gif png webp avif).include?(@type) ? send("parse_animated_for_#{@type}") : nil
435
440
  end
436
441
 
437
442
  def fetch_using_base64(uri)
@@ -547,6 +552,8 @@ class FastImage
547
552
  case @stream.peek(12)[4..-1]
548
553
  when "ftypavif"
549
554
  :avif
555
+ when "ftypavis"
556
+ :avif
550
557
  when "ftypheic"
551
558
  :heic
552
559
  when "ftypmif1"
@@ -563,9 +570,9 @@ class FastImage
563
570
  when /\s\s|\s<|<[?!]/, 0xef.chr + 0xbb.chr
564
571
  # Peek 10 more chars each time, and if end of file is reached just raise
565
572
  # unknown. We assume the <svg tag cannot be within 10 chars of the end of
566
- # the file, and is within the first 250 chars.
573
+ # the file, and is within the first 1000 chars.
567
574
  begin
568
- :svg if (1..25).detect {|n| @stream.peek(10 * n).include?("<svg")}
575
+ :svg if (1..100).detect {|n| @stream.peek(10 * n).include?("<svg")}
569
576
  rescue FiberError
570
577
  nil
571
578
  end
@@ -643,9 +650,9 @@ class FastImage
643
650
  when "ispe"
644
651
  handle_ispe_box(box_size, index)
645
652
  when "mdat"
646
- throw :finish
653
+ @stream.skip(box_size)
647
654
  else
648
- @stream.read(box_size)
655
+ @stream.skip(box_size)
649
656
  end
650
657
 
651
658
  index += 1
@@ -723,6 +730,7 @@ class FastImage
723
730
  def read_box_header!
724
731
  size = read_uint32!
725
732
  type = @stream.read(4)
733
+ size = read_uint64! - 8 if size == 1
726
734
  [type, size - 8]
727
735
  end
728
736
 
@@ -737,6 +745,10 @@ class FastImage
737
745
  def read_uint32!
738
746
  @stream.read(4).unpack("N")[0]
739
747
  end
748
+
749
+ def read_uint64!
750
+ @stream.read(8).unpack("Q>")[0]
751
+ end
740
752
  end
741
753
 
742
754
  def parse_size_for_avif
@@ -1088,4 +1100,43 @@ class FastImage
1088
1100
  gif = Gif.new(@stream)
1089
1101
  gif.animated?
1090
1102
  end
1103
+
1104
+ def parse_animated_for_png
1105
+ # Signature (8) + IHDR chunk (4 + 4 + 13 + 4)
1106
+ @stream.read(33)
1107
+
1108
+ loop do
1109
+ length = @stream.read(4).unpack("L>")[0]
1110
+ type = @stream.read(4)
1111
+
1112
+ case type
1113
+ when "acTL"
1114
+ return true
1115
+ when "IDAT"
1116
+ return false
1117
+ end
1118
+
1119
+ @stream.skip(length + 4)
1120
+ end
1121
+ end
1122
+
1123
+ def parse_animated_for_webp
1124
+ vp8 = @stream.read(16)[12..15]
1125
+ _len = @stream.read(4).unpack("V")
1126
+ case vp8
1127
+ when "VP8 "
1128
+ false
1129
+ when "VP8L"
1130
+ false
1131
+ when "VP8X"
1132
+ flags = @stream.read(4).unpack("C")[0]
1133
+ flags & 2 > 0
1134
+ else
1135
+ nil
1136
+ end
1137
+ end
1138
+
1139
+ def parse_animated_for_avif
1140
+ @stream.peek(12)[4..-1] == "ftypavis"
1141
+ end
1091
1142
  end
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.6
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Sykes
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-28 00:00:00.000000000 Z
11
+ date: 2023-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fakeweb-fi
@@ -72,17 +72,17 @@ 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
82
82
  licenses:
83
83
  - MIT
84
84
  metadata: {}
85
- post_install_message:
85
+ post_install_message:
86
86
  rdoc_options:
87
87
  - "--charset=UTF-8"
88
88
  require_paths:
@@ -98,8 +98,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  requirements: []
101
- rubygems_version: 3.1.6
102
- signing_key:
101
+ rubygems_version: 3.3.7
102
+ signing_key:
103
103
  specification_version: 4
104
104
  summary: FastImage - Image info fast
105
105
  test_files: []
data/README.textile DELETED
@@ -1,163 +0,0 @@
1
- !https://img.shields.io/gem/dt/fastimage.svg!:https://rubygems.org/gems/fastimage
2
- !https://travis-ci.org/sdsykes/fastimage.svg?branch=master!:https://travis-ci.org/sdsykes/fastimage
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