Skip to content

Commit

Permalink
FEATURE: Adds animated? support for webp and avif images
Browse files Browse the repository at this point in the history
  • Loading branch information
xfalcox committed May 22, 2023
1 parent b65d75c commit 5528ef9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/fastimage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def parse_size

def parse_animated
@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

def fetch_using_base64(uri)
Expand Down Expand Up @@ -551,6 +551,8 @@ def parse_type
case @stream.peek(12)[4..-1]
when "ftypavif"
:avif
when "ftypavis"
:avif
when "ftypheic"
:heic
when "ftypmif1"
Expand Down Expand Up @@ -1092,4 +1094,24 @@ def parse_animated_for_gif
gif = Gif.new(@stream)
gif.animated?
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
Binary file added test/fixtures/avif/red_green_flash.avif
Binary file not shown.
Binary file added test/fixtures/webp_animated.webp
Binary file not shown.
6 changes: 6 additions & 0 deletions test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"webp_vp8x.webp" => [:webp, [386, 395]],
"webp_vp8l.webp" => [:webp, [386, 395]],
"webp_vp8.webp" => [:webp, [550, 368]],
"webp_animated.webp" => [:webp, [1280, 531]],
"test.svg" => [:svg, [200, 300]],
"test_partial_viewport.svg" => [:svg, [860, 400]],
"test2.svg" => [:svg, [366, 271]],
Expand All @@ -53,6 +54,7 @@
"avif/hato.avif" => [:avif, [3082, 2048]],
"avif/fox.avif" => [:avif, [1204, 799]],
"avif/kimono.avif" => [:avif, [722, 1024]],
"avif/red_green_flash.avif" => [:avif, [256, 256]],
}

BadFixtures = [
Expand Down Expand Up @@ -124,6 +126,10 @@ def test_should_report_animated_correctly
assert_equal false, FastImage.animated?(TestUrl + "test.gif")
assert_equal true, FastImage.animated?(TestUrl + "animated.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

def test_should_return_nil_on_fetch_failure
Expand Down

0 comments on commit 5528ef9

Please sign in to comment.