fastimage 2.2.7 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +161 -0
- data/lib/fastimage/version.rb +1 -1
- data/lib/fastimage.rb +54 -29
- metadata +5 -5
- data/README.textile +0 -163
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa0337307f5e98ae916e46768e148812f0f6dbb93a1c67d193ca981540779336
|
4
|
+
data.tar.gz: 8600c44df8eed5762ec526709d46e32841a099c0c453e7c255c2a87611dcb822
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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"
|
data/lib/fastimage/version.rb
CHANGED
data/lib/fastimage.rb
CHANGED
@@ -35,17 +35,17 @@
|
|
35
35
|
# === Examples
|
36
36
|
# require 'fastimage'
|
37
37
|
#
|
38
|
-
# FastImage.size("
|
39
|
-
# => [
|
40
|
-
# FastImage.type("http://
|
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://
|
47
|
-
# =>
|
48
|
-
# FastImage.new("http://
|
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("
|
113
|
-
# => [
|
114
|
-
# FastImage.size("http://
|
115
|
-
# => [
|
116
|
-
# FastImage.size("
|
117
|
-
# => [
|
118
|
-
# FastImage.size("
|
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://
|
123
|
+
# FastImage.size("http://switchstep.com/does_not_exist")
|
123
124
|
# => nil
|
124
|
-
# FastImage.size("http://
|
125
|
+
# FastImage.size("http://switchstep.com/does_not_exist", :raise_on_failure=>true)
|
125
126
|
# => raises FastImage::ImageFetchFailure
|
126
|
-
# FastImage.size("http://
|
127
|
+
# FastImage.size("http://switchstep.com/images/favicon.ico", :raise_on_failure=>true)
|
127
128
|
# => [16, 16]
|
128
|
-
# FastImage.size("http://
|
129
|
+
# FastImage.size("http://switchstep.com/foo.ics", :raise_on_failure=>true)
|
129
130
|
# => raises FastImage::UnknownImageType
|
130
|
-
# FastImage.size("http://
|
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://
|
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("
|
159
|
+
# FastImage.type("https://switchstep.com/images/ios.gif")
|
159
160
|
# => :gif
|
160
|
-
# FastImage.type("http://
|
161
|
+
# FastImage.type("http://switchstep.com/images/ss_logo.png")
|
161
162
|
# => :png
|
162
|
-
# FastImage.type("
|
163
|
+
# FastImage.type("https://upload.wikimedia.org/wikipedia/commons/0/09/Jpeg_thumb_artifacts_test.jpg")
|
163
164
|
# => :jpeg
|
164
|
-
# FastImage.type("
|
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://
|
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
|
@@ -435,7 +436,7 @@ class FastImage
|
|
435
436
|
|
436
437
|
def parse_animated
|
437
438
|
@type = parse_type unless @type
|
438
|
-
%i(gif webp avif).include?(@type) ? send("parse_animated_for_#{@type}") : nil
|
439
|
+
%i(gif png webp avif).include?(@type) ? send("parse_animated_for_#{@type}") : nil
|
439
440
|
end
|
440
441
|
|
441
442
|
def fetch_using_base64(uri)
|
@@ -569,9 +570,9 @@ class FastImage
|
|
569
570
|
when /\s\s|\s<|<[?!]/, 0xef.chr + 0xbb.chr
|
570
571
|
# Peek 10 more chars each time, and if end of file is reached just raise
|
571
572
|
# unknown. We assume the <svg tag cannot be within 10 chars of the end of
|
572
|
-
# the file, and is within the first
|
573
|
+
# the file, and is within the first 1000 chars.
|
573
574
|
begin
|
574
|
-
:svg if (1..
|
575
|
+
:svg if (1..100).detect {|n| @stream.peek(10 * n).include?("<svg")}
|
575
576
|
rescue FiberError
|
576
577
|
nil
|
577
578
|
end
|
@@ -649,9 +650,9 @@ class FastImage
|
|
649
650
|
when "ispe"
|
650
651
|
handle_ispe_box(box_size, index)
|
651
652
|
when "mdat"
|
652
|
-
|
653
|
+
@stream.skip(box_size)
|
653
654
|
else
|
654
|
-
@stream.
|
655
|
+
@stream.skip(box_size)
|
655
656
|
end
|
656
657
|
|
657
658
|
index += 1
|
@@ -729,6 +730,7 @@ class FastImage
|
|
729
730
|
def read_box_header!
|
730
731
|
size = read_uint32!
|
731
732
|
type = @stream.read(4)
|
733
|
+
size = read_uint64! - 8 if size == 1
|
732
734
|
[type, size - 8]
|
733
735
|
end
|
734
736
|
|
@@ -743,6 +745,10 @@ class FastImage
|
|
743
745
|
def read_uint32!
|
744
746
|
@stream.read(4).unpack("N")[0]
|
745
747
|
end
|
748
|
+
|
749
|
+
def read_uint64!
|
750
|
+
@stream.read(8).unpack("Q>")[0]
|
751
|
+
end
|
746
752
|
end
|
747
753
|
|
748
754
|
def parse_size_for_avif
|
@@ -1095,6 +1101,25 @@ class FastImage
|
|
1095
1101
|
gif.animated?
|
1096
1102
|
end
|
1097
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
|
+
|
1098
1123
|
def parse_animated_for_webp
|
1099
1124
|
vp8 = @stream.read(16)[12..15]
|
1100
1125
|
_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.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Sykes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
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,10 +72,10 @@ email: sdsykes@gmail.com
|
|
72
72
|
executables: []
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files:
|
75
|
-
- README.
|
75
|
+
- README.md
|
76
76
|
files:
|
77
77
|
- MIT-LICENSE
|
78
|
-
- README.
|
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.
|
101
|
+
rubygems_version: 3.3.7
|
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
|