Skip to content

Commit

Permalink
Merge pull request #144 from c960657/animated-gif
Browse files Browse the repository at this point in the history
Add animated? support for png images
  • Loading branch information
sdsykes committed Sep 28, 2023
2 parents 71469cb + fb319cd commit 1fcc6c8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
21 changes: 20 additions & 1 deletion lib/fastimage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def parse_size

def parse_animated
@type = parse_type unless @type
%i(gif webp avif).include?(@type) ? send("parse_animated_for_#{@type}") : nil
%i(gif png webp avif).include?(@type) ? send("parse_animated_for_#{@type}") : nil
end

def fetch_using_base64(uri)
Expand Down Expand Up @@ -1096,6 +1096,25 @@ def parse_animated_for_gif
gif.animated?
end

def parse_animated_for_png
# Signature (8) + IHDR chunk (4 + 4 + 13 + 4)
@stream.read(33)

loop do
length = @stream.read(4).unpack("L>")[0]
type = @stream.read(4)

case type
when "acTL"
return true
when "IDAT"
return false
end

@stream.skip(length + 4)
end
end

def parse_animated_for_webp
vp8 = @stream.read(16)[12..15]
_len = @stream.read(4).unpack("V")
Expand Down
Binary file added test/fixtures/animated.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"test_v5header.bmp"=>[:bmp, [40, 27]],
"test.gif"=>[:gif, [17, 32]],
"animated.gif"=>[:gif, [400, 400]],
"animated.png"=>[:png, [100, 100]],
"animated_without_gct.gif"=>[:gif, [859, 478]],
"test.jpg"=>[:jpeg, [882, 470]],
"test.png"=>[:png, [30, 20]],
Expand Down Expand Up @@ -123,9 +124,11 @@ def test_should_report_size_correctly
end

def test_should_report_animated_correctly
assert_equal nil, FastImage.animated?(TestUrl + "test.png")
assert_equal nil, FastImage.animated?(TestUrl + "test.jpg")
assert_equal false, FastImage.animated?(TestUrl + "test.png")
assert_equal false, FastImage.animated?(TestUrl + "test.gif")
assert_equal true, FastImage.animated?(TestUrl + "animated.gif")
assert_equal true, FastImage.animated?(TestUrl + "animated.png")
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")
Expand Down

0 comments on commit 1fcc6c8

Please sign in to comment.