mirror of
https://github.com/sdsykes/fastimage.git
synced 2025-10-05 00:02:33 -04:00
Add support for Data Uri Scheme
This commit is contained in:
parent
e8aec702fa
commit
cbc17cbf69
@ -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 "<?"
|
||||
:svg if @stream.peek(100).include?("<svg")
|
||||
end
|
||||
|
||||
|
||||
parsed_type or raise UnknownImageType
|
||||
end
|
||||
|
||||
@ -644,7 +653,7 @@ class FastImage
|
||||
end
|
||||
|
||||
parse_exif_ifd
|
||||
|
||||
|
||||
@orientation ||= 1
|
||||
end
|
||||
|
||||
|
13
test/test.rb
13
test/test.rb
@ -56,6 +56,9 @@ LargeImageFetchLimit = 2 # seconds
|
||||
HTTPSImage = "https://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg"
|
||||
HTTPSImageInfo = [:jpeg, [9545, 6623]]
|
||||
|
||||
DataUriImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAYAAAD0In+KAAAAD0lEQVR42mNk+M9QzwAEAAmGAYCF+yOnAAAAAElFTkSuQmCC"
|
||||
DataUriImageInfo = [:png, [2, 1]]
|
||||
|
||||
GoodFixtures.each do |fn, info|
|
||||
FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :body => 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user