Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/thirty-pumas-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixed an issue where looping GIF animation would stop when converted to WebP
15 changes: 14 additions & 1 deletion packages/astro/src/assets/services/sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,20 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
}
}

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 });
Expand Down
Loading