Skip to content

Commit 60bd02c

Browse files
authored
Fix some webp images improperly marked as animated (#29713)
* Fix some webp images improperly marked as animated * Add unit test for an unanimated webp file in extended file format * Apply linting to webp test
1 parent d4f25e8 commit 60bd02c

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/utils/Image.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ export async function blobIsAnimated(mimeType: string | undefined, blob: Blob):
3131
case "image/webp": {
3232
// Only extended file format WEBP images support animation, so grab the expected data range and verify header.
3333
// Based on https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
34-
const arr = await blob.slice(0, 17).arrayBuffer();
34+
const arr = await blob.slice(0, 21).arrayBuffer();
3535
if (
3636
arrayBufferReadStr(arr, 0, 4) === "RIFF" &&
3737
arrayBufferReadStr(arr, 8, 4) === "WEBP" &&
3838
arrayBufferReadStr(arr, 12, 4) === "VP8X"
3939
) {
40-
const [flags] = arrayBufferRead(arr, 16, 1);
40+
const [flags] = arrayBufferRead(arr, 20, 1);
4141
// Flags: R R I L E X _A_ R (reversed)
4242
const animationFlagMask = 1 << 1;
4343
return (flags & animationFlagMask) != 0;

test/unit-tests/Image-test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ describe("Image", () => {
5151
expect(await blobIsAnimated("image/webp", img)).toBeFalsy();
5252
});
5353

54+
it("Static WEBP in extended file format", async () => {
55+
const img = new Blob([
56+
fs.readFileSync(path.resolve(__dirname, "images", "static-logo-extended-file-format.webp")),
57+
]);
58+
expect(await blobIsAnimated("image/webp", img)).toBeFalsy();
59+
});
60+
5461
it("Animated PNG", async () => {
5562
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.apng"))]);
5663
expect(await blobIsAnimated("image/png", img)).toBeTruthy();
Binary file not shown.

0 commit comments

Comments
 (0)