fastimage 1.2.8 → 1.2.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README +2 -2
- data/README.textile +9 -5
- data/VERSION.yml +1 -1
- data/lib/fastimage.rb +26 -10
- data/test/test.rb +16 -0
- metadata +3 -3
data/README
CHANGED
@@ -31,7 +31,7 @@ Installation
|
|
31
31
|
|
32
32
|
h4. Gem
|
33
33
|
|
34
|
-
|
34
|
+
gem install fastimage
|
35
35
|
|
36
36
|
h4. Rails
|
37
37
|
|
@@ -40,7 +40,7 @@ Install the gem as above, and configure it in your environment.rb file as below:
|
|
40
40
|
...
|
41
41
|
Rails::Initializer.run do |config|
|
42
42
|
...
|
43
|
-
config.gem "
|
43
|
+
config.gem "fastimage", :lib=>"fastimage"
|
44
44
|
...
|
45
45
|
end
|
46
46
|
...
|
data/README.textile
CHANGED
@@ -8,7 +8,7 @@ Your app needs to find the size or type of an image. This could be for adding w
|
|
8
8
|
|
9
9
|
But the image is not locally stored - it's on another asset server, or in the cloud - at Amazon S3 for example.
|
10
10
|
|
11
|
-
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 image types, 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
|
11
|
+
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 image types, 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.
|
12
12
|
|
13
13
|
FastImage does this minimal fetch for image types GIF, JPEG, PNG and BMP. And it doesn't rely on installing external libraries such as RMagick (which relies on ImageMagick or GraphicsMagick) or ImageScience (which relies on FreeImage).
|
14
14
|
|
@@ -16,6 +16,9 @@ You only need supply the uri, and FastImage will do the rest.
|
|
16
16
|
|
17
17
|
Fastimage can also read local (and other) files, and uses the open-uri library to do so.
|
18
18
|
|
19
|
+
And new in v1.2.9, FastImage will automatically read from any object that responds to :read - for
|
20
|
+
instance an IO object if that is passed instead of a URI.
|
21
|
+
|
19
22
|
h2. Examples
|
20
23
|
|
21
24
|
<pre>
|
@@ -37,19 +40,19 @@ h4. Gem
|
|
37
40
|
|
38
41
|
<pre>
|
39
42
|
<code>
|
40
|
-
|
43
|
+
gem install fastimage
|
41
44
|
</code>
|
42
45
|
</pre>
|
43
46
|
|
44
47
|
h4. Rails
|
45
48
|
|
46
|
-
Install the gem as above, and configure it in your environment.rb file as below:
|
49
|
+
Install the gem as above, and add it to your Gemfile if you are using bundler, or configure it in your environment.rb file as below:
|
47
50
|
<pre>
|
48
51
|
<code>
|
49
52
|
...
|
50
53
|
Rails::Initializer.run do |config|
|
51
54
|
...
|
52
|
-
config.gem "fastimage"
|
55
|
+
config.gem "fastimage"
|
53
56
|
...
|
54
57
|
end
|
55
58
|
...
|
@@ -84,5 +87,6 @@ h2. References
|
|
84
87
|
* "http://www.anttikupila.com/flash/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/
|
85
88
|
* "http://imagesize.rubyforge.org/":http://imagesize.rubyforge.org/
|
86
89
|
|
90
|
+
h2. Licence
|
87
91
|
|
88
|
-
|
92
|
+
MIT, see file MIT_LICENCE
|
data/VERSION.yml
CHANGED
data/lib/fastimage.rb
CHANGED
@@ -13,6 +13,9 @@
|
|
13
13
|
# it has enough. This is possibly a useful bandwidth-saving feature if the file is on a network
|
14
14
|
# attached disk rather than truly local.
|
15
15
|
#
|
16
|
+
# New in v1.2.9, FastImage will automatically read from any object that responds to :read - for
|
17
|
+
# instance an IO object if that is passed instead of a URI.
|
18
|
+
#
|
16
19
|
# === Examples
|
17
20
|
# require 'fastimage'
|
18
21
|
#
|
@@ -22,6 +25,8 @@
|
|
22
25
|
# => :png
|
23
26
|
# FastImage.type("/some/local/file.gif")
|
24
27
|
# => :gif
|
28
|
+
# File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
|
29
|
+
# => :gif
|
25
30
|
#
|
26
31
|
# === References
|
27
32
|
# * http://snippets.dzone.com/posts/show/805
|
@@ -121,6 +126,8 @@ class FastImage
|
|
121
126
|
# => :jpeg
|
122
127
|
# FastImage.type("http://pennysmalls.com/does_not_exist")
|
123
128
|
# => nil
|
129
|
+
# File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
|
130
|
+
# => :gif
|
124
131
|
#
|
125
132
|
# === Supported options
|
126
133
|
# [:timeout]
|
@@ -136,15 +143,20 @@ class FastImage
|
|
136
143
|
@property = options[:type_only] ? :type : :size
|
137
144
|
@timeout = options[:timeout] || DefaultTimeout
|
138
145
|
@uri = uri
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
fetch_using_open_uri
|
146
|
+
|
147
|
+
if uri.respond_to?(:read)
|
148
|
+
fetch_using_read(uri)
|
143
149
|
else
|
144
|
-
|
145
|
-
|
146
|
-
|
150
|
+
begin
|
151
|
+
@parsed_uri = URI.parse(uri)
|
152
|
+
rescue URI::InvalidURIError
|
147
153
|
fetch_using_open_uri
|
154
|
+
else
|
155
|
+
if @parsed_uri.scheme == "http" || @parsed_uri.scheme == "https"
|
156
|
+
fetch_using_http
|
157
|
+
else
|
158
|
+
fetch_using_open_uri
|
159
|
+
end
|
148
160
|
end
|
149
161
|
end
|
150
162
|
raise SizeNotFound if options[:raise_on_failure] && @property == :size && !@size
|
@@ -177,11 +189,15 @@ class FastImage
|
|
177
189
|
@http.read_timeout = @timeout
|
178
190
|
end
|
179
191
|
|
192
|
+
def fetch_using_read(readable)
|
193
|
+
while str = readable.read(LocalFileChunkSize)
|
194
|
+
break if parse_packet(str)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
180
198
|
def fetch_using_open_uri
|
181
199
|
open(@uri) do |s|
|
182
|
-
|
183
|
-
break if parse_packet(str)
|
184
|
-
end
|
200
|
+
fetch_using_read(s)
|
185
201
|
end
|
186
202
|
end
|
187
203
|
|
data/test/test.rb
CHANGED
@@ -100,6 +100,22 @@ class FastImageTest < Test::Unit::TestCase
|
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
+
def test_should_report_type_correctly_for_ios
|
104
|
+
GoodFixtures.each do |fn, info|
|
105
|
+
File.open(File.join(FixturePath, fn), "r") do |io|
|
106
|
+
assert_equal info[0], FastImage.type(io)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_should_report_size_correctly_for_ios
|
112
|
+
GoodFixtures.each do |fn, info|
|
113
|
+
File.open(File.join(FixturePath, fn), "r") do |io|
|
114
|
+
assert_equal info[1], FastImage.size(io)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
103
119
|
def test_should_report_size_correctly_for_local_files_with_path_that_has_spaces
|
104
120
|
Dir.chdir(PathHere) do
|
105
121
|
assert_equal GoodFixtures["test.bmp"][1], FastImage.size(File.join("fixtures", "folder with spaces", "test.bmp"))
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fastimage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 9
|
10
|
+
version: 1.2.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Stephen Sykes
|