Skip to content

Fix some webp images improperly marked as animated #29713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/utils/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export async function blobIsAnimated(mimeType: string | undefined, blob: Blob):
case "image/webp": {
// Only extended file format WEBP images support animation, so grab the expected data range and verify header.
// Based on https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
const arr = await blob.slice(0, 17).arrayBuffer();
const arr = await blob.slice(0, 21).arrayBuffer();
if (
arrayBufferReadStr(arr, 0, 4) === "RIFF" &&
arrayBufferReadStr(arr, 8, 4) === "WEBP" &&
arrayBufferReadStr(arr, 12, 4) === "VP8X"
) {
const [flags] = arrayBufferRead(arr, 16, 1);
const [flags] = arrayBufferRead(arr, 20, 1);
// Flags: R R I L E X _A_ R (reversed)
const animationFlagMask = 1 << 1;
return (flags & animationFlagMask) != 0;
Expand Down
5 changes: 5 additions & 0 deletions test/unit-tests/Image-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ describe("Image", () => {
expect(await blobIsAnimated("image/webp", img)).toBeFalsy();
});

it("Static WEBP in extended file format", async () => {
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo-extended-file-format.webp"))]);
expect(await blobIsAnimated("image/webp", img)).toBeFalsy();
});

it("Animated PNG", async () => {
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.apng"))]);
expect(await blobIsAnimated("image/png", img)).toBeTruthy();
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that got a good laugh from me

Binary file not shown.
Loading