diff --git a/lib/fastimage.rb b/lib/fastimage.rb index 5d0b1e4..7e7b3c7 100644 --- a/lib/fastimage.rb +++ b/lib/fastimage.rb @@ -28,7 +28,7 @@ # or referrer which some servers require. Pass an :http_header argument to specify headers, # e.g., :http_header => {'User-Agent' => 'Fake Browser'}. # -# FastImage can give you information about the parsed display orientation of an image with Exif +# FastImage can give you information about the parsed display orientation of an image with Exif # data (jpeg or tiff). # # === Examples @@ -59,6 +59,8 @@ require 'addressable/uri' require 'delegate' require 'pathname' require 'zlib' +require 'base64' + class FastImage attr_reader :size, :type, :content_length, :orientation @@ -184,6 +186,8 @@ class FastImage if uri.respond_to?(:read) fetch_using_read(uri) + elsif uri.start_with?('data:') + fetch_using_base64(uri) else begin @parsed_uri = Addressable::URI.parse(uri) @@ -351,7 +355,7 @@ class FastImage else @orientation = 1 end - + instance_variable_set("@#{@property}", result) else raise CannotParseImage @@ -366,6 +370,11 @@ class FastImage send("parse_size_for_#{@type}") end + def fetch_using_base64(uri) + data = uri.split(',')[1] + fetch_using_read StringIO.new(Base64.decode64(data)) + end + module StreamUtil # :nodoc: def read_byte read(1)[0].ord @@ -468,7 +477,7 @@ class FastImage when " File.join(FixturePath, fn)) end @@ -358,11 +361,19 @@ class FastImageTest < Test::Unit::TestCase url = "#{TestUrl}test.gif" assert_equal 1, FastImage.new(url).orientation end - + def test_should_raise_when_handling_invalid_ico_files stringio = StringIO.new("\x00\x00003") assert_raises(FastImage::UnknownImageType) do FastImage.type(stringio, :raise_on_failure => true) end end + + def test_should_support_data_uri_scheme_images + assert_equal DataUriImageInfo[0], FastImage.type(DataUriImage) + assert_equal DataUriImageInfo[1], FastImage.size(DataUriImage) + assert_raises(FastImage::ImageFetchFailure) do + FastImage.type("data:", :raise_on_failure => true) + end + end end