diff --git a/MIT_LICENCE b/MIT_LICENCE index dcf7764..3e479cc 100644 --- a/MIT_LICENCE +++ b/MIT_LICENCE @@ -1,4 +1,4 @@ -Copyright (c) 2008 Stephen Sykes +Copyright (c) 2008-2011 Stephen Sykes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/README b/README index 50b39a2..eb604a1 100644 --- a/README +++ b/README @@ -31,7 +31,7 @@ Installation h4. Gem - sudo gem install sdsykes-fastimage -s http://gems.github.com + gem install fastimage h4. Rails @@ -40,7 +40,7 @@ Install the gem as above, and configure it in your environment.rb file as below: ... Rails::Initializer.run do |config| ... - config.gem "sdsykes-fastimage", :lib=>"fastimage" + config.gem "fastimage", :lib=>"fastimage" ... end ... diff --git a/README.textile b/README.textile index 28d0a0c..8685ea7 100644 --- a/README.textile +++ b/README.textile @@ -8,7 +8,7 @@ Your app needs to find the size or type of an image. This could be for adding w But the image is not locally stored - it's on another asset server, or in the cloud - at Amazon S3 for example. -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. +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. 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). @@ -16,6 +16,9 @@ You only need supply the uri, and FastImage will do the rest. Fastimage can also read local (and other) files, and uses the open-uri library to do so. +And new in v1.2.9, FastImage will automatically read from any object that responds to :read - for +instance an IO object if that is passed instead of a URI. + h2. Examples
@@ -37,19 +40,19 @@ h4. Gem
 
 
 
-  sudo gem install fastimage
+  gem install fastimage
 
 
h4. Rails -Install the gem as above, and configure it in your environment.rb file as below: +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:
 
 ...
 Rails::Initializer.run do |config|
   ...
-  config.gem "fastimage", :lib=>"fastimage"
+  config.gem "fastimage"
   ...
 end
 ...
@@ -84,5 +87,6 @@ h2. References
 * "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/
 * "http://imagesize.rubyforge.org/":http://imagesize.rubyforge.org/
 
+h2. Licence
 
-(c) 2009 Stephen Sykes
\ No newline at end of file
+MIT, see file MIT_LICENCE
diff --git a/VERSION.yml b/VERSION.yml
index 0a66fde..f39418d 100644
--- a/VERSION.yml
+++ b/VERSION.yml
@@ -1,5 +1,5 @@
 --- 
-:patch: 8
+:patch: 9
 :major: 1
 :build: 
 :minor: 2
diff --git a/fastimage.gemspec b/fastimage.gemspec
index eb2ba4d..8d42ce0 100644
--- a/fastimage.gemspec
+++ b/fastimage.gemspec
@@ -1,6 +1,6 @@
 Gem::Specification.new do |s|
   s.name = %q{fastimage}
-  s.version = "1.2.8"
+  s.version = "1.2.9"
 
   s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
   s.authors = ["Stephen Sykes"]
diff --git a/lib/fastimage.rb b/lib/fastimage.rb
index 9ea0634..2e5e130 100644
--- a/lib/fastimage.rb
+++ b/lib/fastimage.rb
@@ -13,6 +13,9 @@
 # it has enough. This is possibly a useful bandwidth-saving feature if the file is on a network
 # attached disk rather than truly local.
 #
+# New in v1.2.9, FastImage will automatically read from any object that responds to :read - for 
+# instance an IO object if that is passed instead of a URI.
+#
 # === Examples
 #   require 'fastimage'
 #
@@ -22,6 +25,8 @@
 #   => :png
 #   FastImage.type("/some/local/file.gif")
 #   => :gif
+#   File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
+#   => :gif
 #
 # === References
 # * http://snippets.dzone.com/posts/show/805
@@ -121,6 +126,8 @@ def self.size(uri, options={})
   #   => :jpeg
   #   FastImage.type("http://pennysmalls.com/does_not_exist")
   #   => nil
+  #   File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
+  #   => :gif
   #
   # === Supported options
   # [:timeout]
@@ -136,15 +143,20 @@ def initialize(uri, options={})
     @property = options[:type_only] ? :type : :size
     @timeout = options[:timeout] || DefaultTimeout
     @uri = uri
-    begin
-      @parsed_uri = URI.parse(uri)
-    rescue URI::InvalidURIError
-      fetch_using_open_uri
+    
+    if uri.respond_to?(:read)
+      fetch_using_read(uri)
     else
-      if @parsed_uri.scheme == "http" || @parsed_uri.scheme == "https"
-        fetch_using_http
-      else
+      begin
+        @parsed_uri = URI.parse(uri)
+      rescue URI::InvalidURIError
         fetch_using_open_uri
+      else
+        if @parsed_uri.scheme == "http" || @parsed_uri.scheme == "https"
+          fetch_using_http
+        else
+          fetch_using_open_uri
+        end
       end
     end
     raise SizeNotFound if options[:raise_on_failure] && @property == :size && !@size
@@ -177,11 +189,15 @@ def setup_http
     @http.read_timeout = @timeout
   end
 
+  def fetch_using_read(readable)
+    while str = readable.read(LocalFileChunkSize)
+      break if parse_packet(str)
+    end
+  end
+
   def fetch_using_open_uri
     open(@uri) do |s|
-      while str = s.read(LocalFileChunkSize)
-        break if parse_packet(str)
-      end
+      fetch_using_read(s)
     end
   end
 
diff --git a/test/test.rb b/test/test.rb
index a6bf7d6..bdd10a9 100644
--- a/test/test.rb
+++ b/test/test.rb
@@ -100,6 +100,22 @@ def test_should_report_size_correctly_for_local_files
     end    
   end
 
+  def test_should_report_type_correctly_for_ios
+    GoodFixtures.each do |fn, info|
+      File.open(File.join(FixturePath, fn), "r") do |io|
+        assert_equal info[0], FastImage.type(io)
+      end
+    end
+  end
+  
+  def test_should_report_size_correctly_for_ios
+    GoodFixtures.each do |fn, info|
+      File.open(File.join(FixturePath, fn), "r") do |io|
+        assert_equal info[1], FastImage.size(io)
+      end
+    end
+  end
+
   def test_should_report_size_correctly_for_local_files_with_path_that_has_spaces
     Dir.chdir(PathHere) do
       assert_equal GoodFixtures["test.bmp"][1], FastImage.size(File.join("fixtures", "folder with spaces", "test.bmp"))