Merge pull request #141 from xfalcox/animated_for_webp_and_avif

FEATURE: Adds animated? support for webp and avif images
This commit is contained in:
Sam 2023-05-24 08:47:57 +10:00 committed by GitHub
commit 81b5b7d3df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 2 deletions

View File

@ -1,3 +1,9 @@
Version 2.2.7
23-May-2023
- FEATURE: Adds animated? support for webp and avif images
Version 2.2.6 Version 2.2.6
16-December-2021 16-December-2021

View File

@ -435,7 +435,7 @@ class FastImage
def parse_animated def parse_animated
@type = parse_type unless @type @type = parse_type unless @type
@type == :gif ? send("parse_animated_for_#{@type}") : nil %i(gif webp avif).include?(@type) ? send("parse_animated_for_#{@type}") : nil
end end
def fetch_using_base64(uri) def fetch_using_base64(uri)
@ -551,6 +551,8 @@ class FastImage
case @stream.peek(12)[4..-1] case @stream.peek(12)[4..-1]
when "ftypavif" when "ftypavif"
:avif :avif
when "ftypavis"
:avif
when "ftypheic" when "ftypheic"
:heic :heic
when "ftypmif1" when "ftypmif1"
@ -1092,4 +1094,24 @@ class FastImage
gif = Gif.new(@stream) gif = Gif.new(@stream)
gif.animated? gif.animated?
end end
def parse_animated_for_webp
vp8 = @stream.read(16)[12..15]
_len = @stream.read(4).unpack("V")
case vp8
when "VP8 "
false
when "VP8L"
false
when "VP8X"
flags = @stream.read(4).unpack("C")[0]
flags & 2 > 0
else
nil
end
end
def parse_animated_for_avif
@stream.peek(12)[4..-1] == "ftypavis"
end
end end

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
class FastImage class FastImage
VERSION = '2.2.6' VERSION = '2.2.7'
end end

BIN
test/fixtures/avif/red_green_flash.avif vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
test/fixtures/webp_animated.webp vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@ -34,6 +34,7 @@ GoodFixtures = {
"webp_vp8x.webp" => [:webp, [386, 395]], "webp_vp8x.webp" => [:webp, [386, 395]],
"webp_vp8l.webp" => [:webp, [386, 395]], "webp_vp8l.webp" => [:webp, [386, 395]],
"webp_vp8.webp" => [:webp, [550, 368]], "webp_vp8.webp" => [:webp, [550, 368]],
"webp_animated.webp" => [:webp, [400, 400]],
"test.svg" => [:svg, [200, 300]], "test.svg" => [:svg, [200, 300]],
"test_partial_viewport.svg" => [:svg, [860, 400]], "test_partial_viewport.svg" => [:svg, [860, 400]],
"test2.svg" => [:svg, [366, 271]], "test2.svg" => [:svg, [366, 271]],
@ -53,6 +54,7 @@ GoodFixtures = {
"avif/hato.avif" => [:avif, [3082, 2048]], "avif/hato.avif" => [:avif, [3082, 2048]],
"avif/fox.avif" => [:avif, [1204, 799]], "avif/fox.avif" => [:avif, [1204, 799]],
"avif/kimono.avif" => [:avif, [722, 1024]], "avif/kimono.avif" => [:avif, [722, 1024]],
"avif/red_green_flash.avif" => [:avif, [256, 256]],
} }
BadFixtures = [ BadFixtures = [
@ -124,6 +126,10 @@ class FastImageTest < Test::Unit::TestCase
assert_equal false, FastImage.animated?(TestUrl + "test.gif") assert_equal false, FastImage.animated?(TestUrl + "test.gif")
assert_equal true, FastImage.animated?(TestUrl + "animated.gif") assert_equal true, FastImage.animated?(TestUrl + "animated.gif")
assert_equal true, FastImage.animated?(TestUrl + "animated_without_gct.gif") assert_equal true, FastImage.animated?(TestUrl + "animated_without_gct.gif")
assert_equal false, FastImage.animated?(TestUrl + "webp_vp8x.webp")
assert_equal true, FastImage.animated?(TestUrl + "webp_animated.webp")
assert_equal false, FastImage.animated?(TestUrl + "avif/hato.avif")
assert_equal true, FastImage.animated?(TestUrl + "avif/red_green_flash.avif")
end end
def test_should_return_nil_on_fetch_failure def test_should_return_nil_on_fetch_failure