From 2996ad843154e1e485f7ed2e0ef3e3fa0a6ad649 Mon Sep 17 00:00:00 2001 From: L4Ph <4ranci0ne@gmail.com> Date: Fri, 9 May 2025 23:18:12 +0900 Subject: [PATCH 1/2] feat(sharp): add support for converting animated GIFs to animated WebP --- packages/astro/src/assets/services/sharp.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/assets/services/sharp.ts b/packages/astro/src/assets/services/sharp.ts index bbae39eb093a..c6088ad9959c 100644 --- a/packages/astro/src/assets/services/sharp.ts +++ b/packages/astro/src/assets/services/sharp.ts @@ -108,7 +108,20 @@ const sharpService: LocalImageService = { } } - result.toFormat(transform.format as keyof FormatEnum, { quality: quality }); + const isGifInput = + inputBuffer[0] === 0x47 && // 'G' + inputBuffer[1] === 0x49 && // 'I' + inputBuffer[2] === 0x46 && // 'F' + inputBuffer[3] === 0x38 && // '8' + (inputBuffer[4] === 0x39 || inputBuffer[4] === 0x37) && // '9' or '7' + inputBuffer[5] === 0x61; // 'a' + + if (transform.format === 'webp' && isGifInput) { + // Convert animated GIF to animated WebP with loop=0 (infinite) + result.webp({ quality: typeof quality === 'number' ? quality : undefined, loop: 0 }); + } else { + result.toFormat(transform.format as keyof FormatEnum, { quality }); + } } const { data, info } = await result.toBuffer({ resolveWithObject: true }); From 887157e2e535fa843bd7eec0e6abc3601e52609e Mon Sep 17 00:00:00 2001 From: L4Ph <4ranci0ne@gmail.com> Date: Fri, 9 May 2025 23:25:43 +0900 Subject: [PATCH 2/2] add changeset --- .changeset/thirty-pumas-shop.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/thirty-pumas-shop.md diff --git a/.changeset/thirty-pumas-shop.md b/.changeset/thirty-pumas-shop.md new file mode 100644 index 000000000000..37f00b952a16 --- /dev/null +++ b/.changeset/thirty-pumas-shop.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixed an issue where looping GIF animation would stop when converted to WebP