mirror of
https://github.com/sdsykes/fastimage.git
synced 2025-12-30 00:05:02 -05:00
commit
f92c8afffd
@ -37,6 +37,8 @@ FastImage accepts additional HTTP headers. This can be used to set a user agent
|
||||
|
||||
FastImage can give you information about the parsed display orientation of an image with Exif data (jpeg or tiff).
|
||||
|
||||
FastImage also handles "Data URIs":https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs correctly.
|
||||
|
||||
h2. Security
|
||||
|
||||
As of v1.6.7 FastImage no longer uses @openuri@ to open files, but directly calls @File.open@. Take care to sanitise the strings passed to FastImage; it will try to read from whatever is passed.
|
||||
@ -62,6 +64,8 @@ FastImage.size("http://stephensykes.com/images/ss.com_x.gif", :http_header => {'
|
||||
=> [266, 56]
|
||||
FastImage.new("http://stephensykes.com/images/ExifOrientation3.jpg").orientation
|
||||
=> 3
|
||||
FastImage.size("data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
|
||||
=> [1, 1]
|
||||
</code></pre>
|
||||
|
||||
h2. Installation
|
||||
@ -97,7 +101,7 @@ irb> uri = "http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_13
|
||||
irb> puts Benchmark.measure {open(uri, 'rb') {|fh| p ImageSize.new(fh).size}}
|
||||
[9545, 6623]
|
||||
0.680000 0.250000 0.930000 ( 7.571887)
|
||||
|
||||
|
||||
irb> puts Benchmark.measure {p FastImage.size(uri)}
|
||||
[9545, 6623]
|
||||
0.010000 0.000000 0.010000 ( 0.090640)
|
||||
@ -114,7 +118,7 @@ irb> uri = "http://upload.wikimedia.org/wikipedia/commons/1/11/Shinbutsureijoush
|
||||
irb> puts Benchmark.measure {open(uri, 'rb') {|fh| p ImageSize.new(fh).size}}
|
||||
[1120, 1559]
|
||||
1.080000 0.370000 1.450000 ( 13.766962)
|
||||
|
||||
|
||||
irb> puts Benchmark.measure {p FastImage.size(uri)}
|
||||
[1120, 1559]
|
||||
3.490000 3.810000 7.300000 ( 11.754315)
|
||||
@ -124,8 +128,8 @@ h2. Tests
|
||||
|
||||
You'll need to @gem install fakeweb@ and possibly also @gem install test-unit@ to be able to run the tests.
|
||||
|
||||
bc.. $ ruby test.rb
|
||||
Run options:
|
||||
bc.. $ ruby test.rb
|
||||
Run options:
|
||||
|
||||
# Running tests:
|
||||
|
||||
|
||||
@ -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
|
||||
@ -58,6 +58,7 @@ require 'net/https'
|
||||
require 'delegate'
|
||||
require 'pathname'
|
||||
require 'zlib'
|
||||
require 'base64'
|
||||
require 'uri'
|
||||
|
||||
# see http://stackoverflow.com/questions/5208851/i/41048816#41048816
|
||||
@ -191,6 +192,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 = URI.parse(uri)
|
||||
@ -358,7 +361,7 @@ class FastImage
|
||||
else
|
||||
@orientation = 1
|
||||
end
|
||||
|
||||
|
||||
instance_variable_set("@#{@property}", result)
|
||||
else
|
||||
raise CannotParseImage
|
||||
@ -373,6 +376,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
|
||||
@ -475,7 +483,7 @@ class FastImage
|
||||
when "<?"
|
||||
:svg if @stream.peek(100).include?("<svg")
|
||||
end
|
||||
|
||||
|
||||
parsed_type or raise UnknownImageType
|
||||
end
|
||||
|
||||
@ -651,7 +659,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,13 +361,21 @@ 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
|
||||
|
||||
def test_should_work_with_domains_with_underscores
|
||||
assert_equal :gif, FastImage.type("http://foo_bar.inbro.net/images/p.gif")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user