Skip to content

Commit 245a7d4

Browse files
authored
simplify gif image detection in sharp service (#15161)
1 parent 9b4fe82 commit 245a7d4

1 file changed

Lines changed: 4 additions & 9 deletions

File tree

  • packages/astro/src/assets/services

packages/astro/src/assets/services/sharp.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
7171
// always call rotate to adjust for EXIF data orientation
7272
result.rotate();
7373

74+
// get some information about the input
75+
const { format } = await result.metadata();
76+
7477
// If `fit` isn't set then use old behavior:
7578
// - Do not use both width and height for resizing, and prioritize width over height
7679
// - Allow enlarging images
@@ -108,15 +111,7 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
108111
}
109112
}
110113

111-
const isGifInput =
112-
inputBuffer[0] === 0x47 && // 'G'
113-
inputBuffer[1] === 0x49 && // 'I'
114-
inputBuffer[2] === 0x46 && // 'F'
115-
inputBuffer[3] === 0x38 && // '8'
116-
(inputBuffer[4] === 0x39 || inputBuffer[4] === 0x37) && // '9' or '7'
117-
inputBuffer[5] === 0x61; // 'a'
118-
119-
if (transform.format === 'webp' && isGifInput) {
114+
if (transform.format === 'webp' && format === 'gif') {
120115
// Convert animated GIF to animated WebP with loop=0 (infinite)
121116
result.webp({ quality: typeof quality === 'number' ? quality : undefined, loop: 0 });
122117
} else {

0 commit comments

Comments
 (0)