fastimage 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +74 -0
- data/README.textile +88 -0
- data/Rakefile +18 -0
- data/VERSION.yml +4 -0
- data/lib/fastimage.rb +280 -0
- data/test/fixtures/faulty.jpg +0 -0
- data/test/fixtures/test.bmp +0 -0
- data/test/fixtures/test.gif +0 -0
- data/test/fixtures/test.ico +0 -0
- data/test/fixtures/test.jpg +0 -0
- data/test/fixtures/test.png +0 -0
- data/test/test.rb +112 -0
- metadata +67 -0
data/README
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
FastImage
|
2
|
+
|
3
|
+
FastImage finds the size or type of an image given its uri by fetching as little as needed
|
4
|
+
|
5
|
+
The problem
|
6
|
+
|
7
|
+
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.
|
8
|
+
|
9
|
+
But the image is not locally stored - it's on another asset server, or in the cloud - at Amazon S3 for example.
|
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 most of the image to find the size.
|
12
|
+
|
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
|
+
|
15
|
+
You only need supply the uri, and FastImage will do the rest.
|
16
|
+
|
17
|
+
Fastimage can also read local (and other) files, and uses the open-uri library to do so.
|
18
|
+
|
19
|
+
Examples
|
20
|
+
|
21
|
+
require 'fastimage'
|
22
|
+
|
23
|
+
FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
|
24
|
+
=> [266, 56] # width, height
|
25
|
+
FastImage.type("http://stephensykes.com/images/pngimage")
|
26
|
+
=> :png
|
27
|
+
FastImage.type("/some/local/file.gif")
|
28
|
+
=> :gif
|
29
|
+
|
30
|
+
Installation
|
31
|
+
|
32
|
+
h4. Gem
|
33
|
+
|
34
|
+
sudo gem install sdsykes-fastimage -s http://gems.github.com
|
35
|
+
|
36
|
+
h4. Rails
|
37
|
+
|
38
|
+
Install the gem as above, and configure it in your environment.rb file as below:
|
39
|
+
|
40
|
+
...
|
41
|
+
Rails::Initializer.run do |config|
|
42
|
+
...
|
43
|
+
config.gem "sdsykes-fastimage", :lib=>"fastimage"
|
44
|
+
...
|
45
|
+
end
|
46
|
+
...
|
47
|
+
|
48
|
+
Then you're off - just use FastImage.size() and FastImage.type() in your code as in the examples.
|
49
|
+
|
50
|
+
Documentation
|
51
|
+
|
52
|
+
"http://rdoc.info/projects/sdsykes/fastimage":http://rdoc.info/projects/sdsykes/fastimage
|
53
|
+
|
54
|
+
Tests
|
55
|
+
|
56
|
+
You'll need to 'sudo gem install fakeweb' to be able to run the tests.
|
57
|
+
|
58
|
+
ruby test/test.rb
|
59
|
+
Loaded suite test/test
|
60
|
+
Started
|
61
|
+
...............
|
62
|
+
Finished in 0.059392 seconds.
|
63
|
+
|
64
|
+
15 tests, 27 assertions, 0 failures, 0 errors
|
65
|
+
|
66
|
+
|
67
|
+
References
|
68
|
+
* "http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/":http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/
|
69
|
+
* "http://snippets.dzone.com/posts/show/805":http://snippets.dzone.com/posts/show/805
|
70
|
+
* "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/
|
71
|
+
* "http://imagesize.rubyforge.org/":http://imagesize.rubyforge.org/
|
72
|
+
|
73
|
+
|
74
|
+
(c) 2009 Stephen Sykes
|
data/README.textile
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
h1. FastImage
|
2
|
+
|
3
|
+
h4. FastImage finds the size or type of an image given its uri by fetching as little as needed
|
4
|
+
|
5
|
+
h2. The problem
|
6
|
+
|
7
|
+
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.
|
8
|
+
|
9
|
+
But the image is not locally stored - it's on another asset server, or in the cloud - at Amazon S3 for example.
|
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 most of the image to find the size.
|
12
|
+
|
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
|
+
|
15
|
+
You only need supply the uri, and FastImage will do the rest.
|
16
|
+
|
17
|
+
Fastimage can also read local (and other) files, and uses the open-uri library to do so.
|
18
|
+
|
19
|
+
h2. Examples
|
20
|
+
|
21
|
+
<pre>
|
22
|
+
<code>
|
23
|
+
require 'fastimage'
|
24
|
+
|
25
|
+
FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
|
26
|
+
=> [266, 56] # width, height
|
27
|
+
FastImage.type("http://stephensykes.com/images/pngimage")
|
28
|
+
=> :png
|
29
|
+
FastImage.type("/some/local/file.gif")
|
30
|
+
=> :gif
|
31
|
+
</code>
|
32
|
+
</pre>
|
33
|
+
|
34
|
+
h2. Installation
|
35
|
+
|
36
|
+
h4. Gem
|
37
|
+
|
38
|
+
<pre>
|
39
|
+
<code>
|
40
|
+
sudo gem install sdsykes-fastimage -s http://gems.github.com
|
41
|
+
</code>
|
42
|
+
</pre>
|
43
|
+
|
44
|
+
h4. Rails
|
45
|
+
|
46
|
+
Install the gem as above, and configure it in your environment.rb file as below:
|
47
|
+
<pre>
|
48
|
+
<code>
|
49
|
+
...
|
50
|
+
Rails::Initializer.run do |config|
|
51
|
+
...
|
52
|
+
config.gem "sdsykes-fastimage", :lib=>"fastimage"
|
53
|
+
...
|
54
|
+
end
|
55
|
+
...
|
56
|
+
</code>
|
57
|
+
</pre>
|
58
|
+
Then you're off - just use FastImage.size() and FastImage.type() in your code as in the examples.
|
59
|
+
|
60
|
+
h2. Documentation
|
61
|
+
|
62
|
+
"http://rdoc.info/projects/sdsykes/fastimage":http://rdoc.info/projects/sdsykes/fastimage
|
63
|
+
|
64
|
+
h2. Tests
|
65
|
+
|
66
|
+
You'll need to 'sudo gem install fakeweb' to be able to run the tests.
|
67
|
+
|
68
|
+
<pre>
|
69
|
+
<code>
|
70
|
+
ruby test/test.rb
|
71
|
+
Loaded suite test/test
|
72
|
+
Started
|
73
|
+
...............
|
74
|
+
Finished in 0.059392 seconds.
|
75
|
+
|
76
|
+
15 tests, 27 assertions, 0 failures, 0 errors
|
77
|
+
</code>
|
78
|
+
</pre>
|
79
|
+
|
80
|
+
|
81
|
+
h2. References
|
82
|
+
* "http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/":http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/
|
83
|
+
* "http://snippets.dzone.com/posts/show/805":http://snippets.dzone.com/posts/show/805
|
84
|
+
* "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
|
+
* "http://imagesize.rubyforge.org/":http://imagesize.rubyforge.org/
|
86
|
+
|
87
|
+
|
88
|
+
(c) 2009 Stephen Sykes
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |s|
|
6
|
+
s.name = "fastimage"
|
7
|
+
s.summary = "FastImage - Image info fast"
|
8
|
+
s.email = "sdsykes@gmail.com"
|
9
|
+
s.homepage = "http://github.com/sdsykes/fastimage"
|
10
|
+
s.description = "FastImage finds the size or type of an image given its uri by fetching as little as needed."
|
11
|
+
s.authors = ["Stephen Sykes"]
|
12
|
+
s.files = FileList["[A-Z]*", "{lib,test}/**/*"]
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://
|
17
|
+
gems.github.com"
|
18
|
+
end
|
data/VERSION.yml
ADDED
data/lib/fastimage.rb
ADDED
@@ -0,0 +1,280 @@
|
|
1
|
+
# FastImage finds the size or type of an image given its uri.
|
2
|
+
# It is careful to only fetch and parse as much of the image as is needed to determine the result.
|
3
|
+
# It does this by using a feature of Net::HTTP that yields strings from the resource being fetched
|
4
|
+
# as soon as the packets arrive.
|
5
|
+
#
|
6
|
+
# No external libraries such as ImageMagick are used here, this is a very lightweight solution to
|
7
|
+
# finding image information.
|
8
|
+
#
|
9
|
+
# FastImage knows about GIF, JPEG, BMP and PNG files.
|
10
|
+
#
|
11
|
+
# FastImage can also read files from the local filesystem by supplying the path instead of a uri.
|
12
|
+
# In this case FastImage uses the open-uri library to read the file in chunks of 256 bytes until
|
13
|
+
# it has enough. This is possibly a useful bandwidth-saving feature if the file is on a network
|
14
|
+
# attached disk rather than truly local.
|
15
|
+
#
|
16
|
+
# === Examples
|
17
|
+
# require 'fastimage'
|
18
|
+
#
|
19
|
+
# FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
|
20
|
+
# => [266, 56]
|
21
|
+
# FastImage.type("http://stephensykes.com/images/pngimage")
|
22
|
+
# => :png
|
23
|
+
# FastImage.type("/some/local/file.gif")
|
24
|
+
# => :gif
|
25
|
+
#
|
26
|
+
# === References
|
27
|
+
# * http://snippets.dzone.com/posts/show/805
|
28
|
+
# * http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/
|
29
|
+
# * http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/
|
30
|
+
# * http://imagesize.rubyforge.org/
|
31
|
+
#
|
32
|
+
require 'net/https'
|
33
|
+
require 'open-uri'
|
34
|
+
|
35
|
+
class FastImage
|
36
|
+
attr_reader :size, :type
|
37
|
+
|
38
|
+
class FastImageException < StandardError # :nodoc:
|
39
|
+
end
|
40
|
+
class MoreCharsNeeded < FastImageException # :nodoc:
|
41
|
+
end
|
42
|
+
class UnknownImageType < FastImageException # :nodoc:
|
43
|
+
end
|
44
|
+
class ImageFetchFailure < FastImageException # :nodoc:
|
45
|
+
end
|
46
|
+
class SizeNotFound < FastImageException # :nodoc:
|
47
|
+
end
|
48
|
+
|
49
|
+
DefaultTimeout = 2
|
50
|
+
|
51
|
+
LocalFileChunkSize = 256
|
52
|
+
|
53
|
+
# Returns an array containing the width and height of the image.
|
54
|
+
# It will return nil if the image could not be fetched, or if the image type was not recognised.
|
55
|
+
#
|
56
|
+
# By default there is a timeout of 2 seconds for opening and reading from a remote server.
|
57
|
+
# This can be changed by passing a :timeout => number_of_seconds in the options.
|
58
|
+
#
|
59
|
+
# If you wish FastImage to raise if it cannot size the image for any reason, then pass
|
60
|
+
# :raise_on_failure => true in the options.
|
61
|
+
#
|
62
|
+
# FastImage knows about GIF, JPEG, BMP and PNG files.
|
63
|
+
#
|
64
|
+
# === Example
|
65
|
+
#
|
66
|
+
# require 'fastimage'
|
67
|
+
#
|
68
|
+
# FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
|
69
|
+
# => [266, 56]
|
70
|
+
# FastImage.size("http://stephensykes.com/images/pngimage")
|
71
|
+
# => [16, 16]
|
72
|
+
# FastImage.size("http://farm4.static.flickr.com/3023/3047236863_9dce98b836.jpg")
|
73
|
+
# => [500, 375]
|
74
|
+
# FastImage.size("http://www-ece.rice.edu/~wakin/images/lena512.bmp")
|
75
|
+
# => [512, 512]
|
76
|
+
# FastImage.size("test/fixtures/test.jpg")
|
77
|
+
# => [882, 470]
|
78
|
+
# FastImage.size("http://pennysmalls.com/does_not_exist")
|
79
|
+
# => nil
|
80
|
+
# FastImage.size("http://pennysmalls.com/does_not_exist", :raise_on_failure=>true)
|
81
|
+
# => raises FastImage::ImageFetchFailure
|
82
|
+
# FastImage.size("http://stephensykes.com/favicon.ico", :raise_on_failure=>true)
|
83
|
+
# => raises FastImage::UnknownImageType
|
84
|
+
# FastImage.size("http://stephensykes.com/favicon.ico", :raise_on_failure=>true, :timeout=>0.01)
|
85
|
+
# => raises FastImage::ImageFetchFailure
|
86
|
+
# FastImage.size("http://stephensykes.com/images/faulty.jpg", :raise_on_failure=>true)
|
87
|
+
# => raises FastImage::SizeNotFound
|
88
|
+
#
|
89
|
+
# === Supported options
|
90
|
+
# [:timeout]
|
91
|
+
# Overrides the default timeout of 2 seconds. Applies both to reading from and opening the http connection.
|
92
|
+
# [:raise_on_failure]
|
93
|
+
# If set to true causes an exception to be raised if the image size cannot be found for any reason.
|
94
|
+
#
|
95
|
+
def self.size(uri, options={})
|
96
|
+
new(uri, options).size
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns an symbol indicating the image type fetched from a uri.
|
100
|
+
# It will return nil if the image could not be fetched, or if the image type was not recognised.
|
101
|
+
#
|
102
|
+
# By default there is a timeout of 2 seconds for opening and reading from a remote server.
|
103
|
+
# This can be changed by passing a :timeout => number_of_seconds in the options.
|
104
|
+
#
|
105
|
+
# If you wish FastImage to raise if it cannot find the type of the image for any reason, then pass
|
106
|
+
# :raise_on_failure => true in the options.
|
107
|
+
#
|
108
|
+
# === Example
|
109
|
+
#
|
110
|
+
# require 'fastimage'
|
111
|
+
#
|
112
|
+
# FastImage.type("http://stephensykes.com/images/ss.com_x.gif")
|
113
|
+
# => :gif
|
114
|
+
# FastImage.type("http://stephensykes.com/images/pngimage")
|
115
|
+
# => :png
|
116
|
+
# FastImage.type("http://farm4.static.flickr.com/3023/3047236863_9dce98b836.jpg")
|
117
|
+
# => :jpeg
|
118
|
+
# FastImage.type("http://www-ece.rice.edu/~wakin/images/lena512.bmp")
|
119
|
+
# => :bmp
|
120
|
+
# FastImage.type("test/fixtures/test.jpg")
|
121
|
+
# => :jpeg
|
122
|
+
# FastImage.type("http://pennysmalls.com/does_not_exist")
|
123
|
+
# => nil
|
124
|
+
#
|
125
|
+
# === Supported options
|
126
|
+
# [:timeout]
|
127
|
+
# Overrides the default timeout of 2 seconds. Applies both to reading from and opening the http connection.
|
128
|
+
# [:raise_on_failure]
|
129
|
+
# If set to true causes an exception to be raised if the image type cannot be found for any reason.
|
130
|
+
#
|
131
|
+
def self.type(uri, options={})
|
132
|
+
new(uri, options.merge(:type_only=>true)).type
|
133
|
+
end
|
134
|
+
|
135
|
+
def initialize(uri, options={})
|
136
|
+
@property = options[:type_only] ? :type : :size
|
137
|
+
@timeout = options[:timeout] || DefaultTimeout
|
138
|
+
@uri = uri
|
139
|
+
@parsed_uri = URI.parse(uri)
|
140
|
+
if @parsed_uri.scheme == "http" || @parsed_uri.scheme == "https"
|
141
|
+
fetch_using_http
|
142
|
+
else
|
143
|
+
fetch_using_open_uri
|
144
|
+
end
|
145
|
+
raise SizeNotFound if options[:raise_on_failure] && @property == :size && !@size
|
146
|
+
rescue Timeout::Error, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET, ImageFetchFailure
|
147
|
+
raise ImageFetchFailure if options[:raise_on_failure]
|
148
|
+
rescue Errno::ENOENT
|
149
|
+
raise ImageFetchFailure if options[:raise_on_failure]
|
150
|
+
rescue UnknownImageType
|
151
|
+
raise UnknownImageType if options[:raise_on_failure]
|
152
|
+
end
|
153
|
+
|
154
|
+
private
|
155
|
+
|
156
|
+
def fetch_using_http
|
157
|
+
setup_http
|
158
|
+
@http.request_get(@parsed_uri.request_uri) do |res|
|
159
|
+
raise ImageFetchFailure unless res.is_a?(Net::HTTPSuccess)
|
160
|
+
res.read_body do |str|
|
161
|
+
break if parse_packet(str)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def setup_http
|
167
|
+
@http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port)
|
168
|
+
@http.use_ssl = (@parsed_uri.scheme == "https")
|
169
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
170
|
+
@http.open_timeout = @timeout
|
171
|
+
@http.read_timeout = @timeout
|
172
|
+
end
|
173
|
+
|
174
|
+
def fetch_using_open_uri
|
175
|
+
open(@uri) do |s|
|
176
|
+
while str = s.read(LocalFileChunkSize)
|
177
|
+
break if parse_packet(str)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# returns true once result is achieved
|
183
|
+
#
|
184
|
+
def parse_packet(str)
|
185
|
+
@str = (@unused_str || "") + str
|
186
|
+
@strpos = 0
|
187
|
+
begin
|
188
|
+
result = send("parse_#{@property}")
|
189
|
+
if result
|
190
|
+
instance_variable_set("@#{@property}", result)
|
191
|
+
true
|
192
|
+
end
|
193
|
+
rescue MoreCharsNeeded
|
194
|
+
false
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
def parse_size
|
199
|
+
@type = parse_type unless @type
|
200
|
+
send("parse_size_for_#{@type}")
|
201
|
+
end
|
202
|
+
|
203
|
+
def get_chars(n)
|
204
|
+
if @strpos + n - 1 >= @str.size
|
205
|
+
@unused_str = @str[@strpos..-1]
|
206
|
+
raise MoreCharsNeeded
|
207
|
+
else
|
208
|
+
result = @str[@strpos..(@strpos + n - 1)]
|
209
|
+
@strpos += n
|
210
|
+
result
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def get_byte
|
215
|
+
get_chars(1).unpack("C")[0]
|
216
|
+
end
|
217
|
+
|
218
|
+
def read_int(str)
|
219
|
+
size_bytes = str.unpack("CC")
|
220
|
+
(size_bytes[0] << 8) + size_bytes[1]
|
221
|
+
end
|
222
|
+
|
223
|
+
def parse_type
|
224
|
+
case get_chars(2)
|
225
|
+
when "BM"
|
226
|
+
:bmp
|
227
|
+
when "GI"
|
228
|
+
:gif
|
229
|
+
when 0xff.chr + 0xd8.chr
|
230
|
+
:jpeg
|
231
|
+
when 0x89.chr + "P"
|
232
|
+
:png
|
233
|
+
else
|
234
|
+
raise UnknownImageType
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def parse_size_for_gif
|
239
|
+
get_chars(9)[4..8].unpack('SS')
|
240
|
+
end
|
241
|
+
|
242
|
+
def parse_size_for_png
|
243
|
+
get_chars(23)[14..22].unpack('NN')
|
244
|
+
end
|
245
|
+
|
246
|
+
def parse_size_for_jpeg
|
247
|
+
loop do
|
248
|
+
@state = case @state
|
249
|
+
when nil
|
250
|
+
get_chars(2)
|
251
|
+
:started
|
252
|
+
when :started
|
253
|
+
get_byte == 0xFF ? :sof : :started
|
254
|
+
when :sof
|
255
|
+
c = get_byte
|
256
|
+
if (0xe0..0xef).include?(c)
|
257
|
+
:skipframe
|
258
|
+
elsif [0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF].detect {|r| r.include? c}
|
259
|
+
:readsize
|
260
|
+
else
|
261
|
+
:skipframe
|
262
|
+
end
|
263
|
+
when :skipframe
|
264
|
+
@skip_chars = read_int(get_chars(2)) - 2
|
265
|
+
:do_skip
|
266
|
+
when :do_skip
|
267
|
+
get_chars(@skip_chars)
|
268
|
+
:started
|
269
|
+
when :readsize
|
270
|
+
s = get_chars(7)
|
271
|
+
return [read_int(s[5..6]), read_int(s[3..4])]
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
def parse_size_for_bmp
|
277
|
+
d = get_chars(27)[12..26]
|
278
|
+
d[0] == 40 ? d[4..-1].unpack('LL') : d[4..8].unpack('SS')
|
279
|
+
end
|
280
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/test/test.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
PathHere = File.dirname(__FILE__)
|
6
|
+
|
7
|
+
require File.join(PathHere, "..", "lib", 'fastimage')
|
8
|
+
|
9
|
+
require 'fakeweb'
|
10
|
+
|
11
|
+
FixturePath = File.join(PathHere, "fixtures")
|
12
|
+
|
13
|
+
GoodFixtures = {
|
14
|
+
"test.bmp"=>[:bmp, [40, 27]],
|
15
|
+
"test.gif"=>[:gif, [17, 32]],
|
16
|
+
"test.jpg"=>[:jpeg, [882, 470]],
|
17
|
+
"test.png"=>[:png, [30, 20]]
|
18
|
+
}
|
19
|
+
|
20
|
+
BadFixtures = [
|
21
|
+
"faulty.jpg",
|
22
|
+
"test.ico"
|
23
|
+
]
|
24
|
+
|
25
|
+
TestUrl = "http://example.nowhere/"
|
26
|
+
|
27
|
+
GoodFixtures.each do |fn, info|
|
28
|
+
FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :file => File.join(FixturePath, fn))
|
29
|
+
end
|
30
|
+
BadFixtures.each do |fn|
|
31
|
+
FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :file => File.join(FixturePath, fn))
|
32
|
+
end
|
33
|
+
|
34
|
+
class FasImageTest < Test::Unit::TestCase
|
35
|
+
def test_should_report_type_correctly
|
36
|
+
GoodFixtures.each do |fn, info|
|
37
|
+
assert_equal info[0], FastImage.type(TestUrl + fn)
|
38
|
+
assert_equal info[0], FastImage.type(TestUrl + fn, :raise_on_failure=>true)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_should_report_size_correctly
|
43
|
+
GoodFixtures.each do |fn, info|
|
44
|
+
assert_equal info[1], FastImage.size(TestUrl + fn)
|
45
|
+
assert_equal info[1], FastImage.size(TestUrl + fn, :raise_on_failure=>true)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_return_nil_on_fetch_failure
|
50
|
+
assert_nil FastImage.size(TestUrl + "does_not_exist")
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_should_return_nil_for_faulty_jpeg_where_size_cannot_be_found
|
54
|
+
assert_nil FastImage.size(TestUrl + "faulty.jpg")
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_return_nil_when_image_type_not_known
|
58
|
+
assert_nil FastImage.size(TestUrl + "test.ico")
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_should_return_nil_if_timeout_occurs
|
62
|
+
assert_nil FastImage.size("http://example.com/does_not_exist", :timeout=>0.001)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_should_raise_when_asked_to_when_size_cannot_be_found
|
66
|
+
assert_raises(FastImage::SizeNotFound) do
|
67
|
+
FastImage.size(TestUrl + "faulty.jpg", :raise_on_failure=>true)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_should_raise_when_asked_to_when_timeout_occurs
|
72
|
+
assert_raises(FastImage::ImageFetchFailure) do
|
73
|
+
FastImage.size("http://example.com/does_not_exist", :timeout=>0.001, :raise_on_failure=>true)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_should_raise_when_asked_when_image_type_not_known
|
78
|
+
assert_raises(FastImage::UnknownImageType) do
|
79
|
+
FastImage.size(TestUrl + "test.ico", :raise_on_failure=>true)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_should_report_type_correctly_for_local_files
|
84
|
+
GoodFixtures.each do |fn, info|
|
85
|
+
assert_equal info[0], FastImage.type(File.join(FixturePath, fn))
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_should_report_size_correctly_for_local_files
|
90
|
+
GoodFixtures.each do |fn, info|
|
91
|
+
assert_equal info[1], FastImage.size(File.join(FixturePath, fn))
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_should_return_nil_on_fetch_failure_for_local_path
|
96
|
+
assert_nil FastImage.size("does_not_exist")
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_should_return_nil_for_faulty_jpeg_where_size_cannot_be_found_for_local_file
|
100
|
+
assert_nil FastImage.size(File.join(FixturePath, "faulty.jpg"))
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_should_return_nil_when_image_type_not_known_for_local_file
|
104
|
+
assert_nil FastImage.size(File.join(FixturePath, "test.ico"))
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_should_raise_when_asked_to_when_size_cannot_be_found_for_local_file
|
108
|
+
assert_raises(FastImage::SizeNotFound) do
|
109
|
+
FastImage.size(File.join(FixturePath, "faulty.jpg"), :raise_on_failure=>true)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fastimage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephen Sykes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-10 00:00:00 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: FastImage finds the size or type of an image given its uri by fetching as little as needed.
|
17
|
+
email: sdsykes@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- README.textile
|
25
|
+
files:
|
26
|
+
- README
|
27
|
+
- README.textile
|
28
|
+
- Rakefile
|
29
|
+
- VERSION.yml
|
30
|
+
- lib/fastimage.rb
|
31
|
+
- test/fixtures/faulty.jpg
|
32
|
+
- test/fixtures/test.bmp
|
33
|
+
- test/fixtures/test.gif
|
34
|
+
- test/fixtures/test.ico
|
35
|
+
- test/fixtures/test.jpg
|
36
|
+
- test/fixtures/test.png
|
37
|
+
- test/test.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/sdsykes/fastimage
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: FastImage - Image info fast
|
66
|
+
test_files:
|
67
|
+
- test/test.rb
|