Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds reading from an IO object

  • Loading branch information...
commit d007cb4df6402051ee4b7b26b21d41eec03815a3 1 parent f1ba3f1
@sdsykes authored
View
2  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
View
4 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
...
View
14 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
<pre>
@@ -37,19 +40,19 @@ h4. Gem
<pre>
<code>
- sudo gem install fastimage
+ gem install fastimage
</code>
</pre>
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:
<pre>
<code>
...
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
+MIT, see file MIT_LICENCE
View
2  VERSION.yml
@@ -1,5 +1,5 @@
---
-:patch: 8
+:patch: 9
:major: 1
:build:
:minor: 2
View
2  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"]
View
36 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
View
16 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"))
Please sign in to comment.
Something went wrong with that request. Please try again.