sdsykes-fastimage 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -4,11 +4,13 @@ h4. FastImage finds the size or type of an image given its uri by fetching as li
4
4
 
5
5
  h2. The problem
6
6
 
7
- Your app needs to find the size or type of an image. But the image is not locally stored - it's on another server, or in the cloud - at Amazon S3 for example.
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
8
 
9
- You don't want to download the entire image, which 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.
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
- FastImage does this minimal fetch for image types GIF, JPEG, PNG and BMP.
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).
12
14
 
13
15
  You only need supply the uri, and FastImage will do the rest.
14
16
 
@@ -31,8 +33,48 @@ h4. Gem
31
33
 
32
34
  sudo gem install sdsykes-fastimage -s http://gems.github.com
33
35
 
36
+ h4. Rails
37
+
38
+ Install the gem as above, and configure it in your environment.rb file as below:
39
+ <pre>
40
+ <code>
41
+ ...
42
+ Rails::Initializer.run do |config|
43
+ ...
44
+ config.gem "fastimage"
45
+ ...
46
+ end
47
+ ...
48
+ </code>
49
+ </pre>
50
+ Then you're off - just use FastImage.size() and FastImage.type() in your code as in the examples.
51
+
34
52
  h2. Documentation
35
53
 
36
54
  "http://rdoc.info/projects/sdsykes/fastimage":http://rdoc.info/projects/sdsykes/fastimage
37
55
 
38
- (c) 2009 Stephen Sykes (sdsykes)
56
+ h2. Tests
57
+
58
+ You'll need to 'sudo gem install fakeweb' to be able to run the tests.
59
+
60
+ <pre>
61
+ <code>
62
+ ruby test/test.rb
63
+ Loaded suite test/test
64
+ Started
65
+ .........
66
+ Finished in 0.055059 seconds.
67
+
68
+ 9 tests, 15 assertions, 0 failures, 0 errors
69
+ </code>
70
+ </pre>
71
+
72
+
73
+ h2. References
74
+ * "http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/":http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/
75
+ * "http://snippets.dzone.com/posts/show/805":http://snippets.dzone.com/posts/show/805
76
+ * "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/
77
+ * "http://imagesize.rubyforge.org/":http://imagesize.rubyforge.org/
78
+
79
+
80
+ (c) 2009 Stephen Sykes
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 2
3
3
  :major: 1
4
4
  :minor: 0
data/lib/fastimage.rb CHANGED
@@ -20,6 +20,7 @@
20
20
  # * http://snippets.dzone.com/posts/show/805
21
21
  # * http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/
22
22
  # * http://pennysmalls.com/2008/08/19/find-jpeg-dimensions-fast-in-ruby/
23
+ # * http://imagesize.rubyforge.org/
23
24
  #
24
25
  require 'net/https'
25
26
 
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/test/test.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'rubygems'
2
+
3
+ require 'test/unit'
4
+
5
+ require 'fastimage'
6
+
7
+ require 'fakeweb'
8
+
9
+ FixturePath = File.join(File.dirname(__FILE__), "fixtures")
10
+
11
+ GoodFixtures = {
12
+ "test.bmp"=>[:bmp, [40, 27]],
13
+ "test.gif"=>[:gif, [17, 32]],
14
+ "test.jpg"=>[:jpg, [882, 470]],
15
+ "test.png"=>[:png, [30, 20]]
16
+ }
17
+
18
+ BadFixtures = [
19
+ "faulty.jpg",
20
+ "test.ico"
21
+ ]
22
+
23
+ TestUrl = "http://example.nowhere/"
24
+
25
+ GoodFixtures.each do |fn, info|
26
+ FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :file => File.join(FixturePath, fn))
27
+ end
28
+ BadFixtures.each do |fn|
29
+ FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :file => File.join(FixturePath, fn))
30
+ end
31
+
32
+ class FasImageTest < Test::Unit::TestCase
33
+ def test_should_report_type_correctly
34
+ GoodFixtures.each do |fn, info|
35
+ assert_equal info[0], FastImage.type(TestUrl + fn)
36
+ end
37
+ end
38
+
39
+ def test_should_report_size_correctly
40
+ GoodFixtures.each do |fn, info|
41
+ assert_equal info[1], FastImage.size(TestUrl + fn)
42
+ end
43
+ end
44
+
45
+ def test_should_return_nil_on_fetch_failure
46
+ assert_nil FastImage.size(TestUrl + "does_not_exist")
47
+ end
48
+
49
+ def test_should_return_nil_for_faulty_jpeg_where_size_cannot_be_found
50
+ assert_nil FastImage.size(TestUrl + "faulty.jpg")
51
+ end
52
+
53
+ def test_should_return_nil_when_image_type_not_known
54
+ assert_nil FastImage.size(TestUrl + "test.ico")
55
+ end
56
+
57
+ def test_should_return_nil_if_timeout_occurs
58
+ assert_nil FastImage.size("http://example.com/does_not_exist", :timeout=>0.001)
59
+ end
60
+
61
+ def test_should_raise_when_asked_to_when_size_cannot_be_found
62
+ assert_raises(FastImage::SizeNotFound) do
63
+ FastImage.size(TestUrl + "faulty.jpg", :raise_on_failure=>true)
64
+ end
65
+ end
66
+
67
+ def test_should_raise_when_asked_to_when_timeout_occurs
68
+ assert_raises(FastImage::ImageFetchFailure) do
69
+ FastImage.size("http://example.com/does_not_exist", :timeout=>0.001, :raise_on_failure=>true)
70
+ end
71
+ end
72
+
73
+ def test_should_raise_when_asked_when_image_type_not_known
74
+ assert_raises(FastImage::UnknownImageType) do
75
+ FastImage.size(TestUrl + "test.ico", :raise_on_failure=>true)
76
+ end
77
+ end
78
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdsykes-fastimage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Sykes
@@ -28,6 +28,14 @@ files:
28
28
  - README.textile
29
29
  - VERSION.yml
30
30
  - lib/fastimage.rb
31
+ - test/fixtures
32
+ - test/fixtures/faulty.jpg
33
+ - test/fixtures/test.bmp
34
+ - test/fixtures/test.gif
35
+ - test/fixtures/test.ico
36
+ - test/fixtures/test.jpg
37
+ - test/fixtures/test.png
38
+ - test/test.rb
31
39
  has_rdoc: true
32
40
  homepage: http://github.com/sdsykes/fastimage
33
41
  post_install_message: