From e68ad369e9399d33f58678d2a3271cd631c2fe6a Mon Sep 17 00:00:00 2001 From: sophie Date: Thu, 19 Sep 2024 01:41:53 +0300 Subject: [PATCH] 1200 byte savings in some cases with gifs --- src/plugins/image-optimization.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/plugins/image-optimization.ts b/src/plugins/image-optimization.ts index d4fc641..5353275 100644 --- a/src/plugins/image-optimization.ts +++ b/src/plugins/image-optimization.ts @@ -35,15 +35,30 @@ export default class ImageOptimization extends Plugin { data = await sharp(file).png({ quality: 100 }).toBuffer(); } if (type == "gif") { - // not heavy optimizations, not a fan tbf, but whatever :shrug: - data = await sharp(file, { animated: true }) - .gif({ colors: 16 }) - .toBuffer(); + const gif = sharp(file, { animated: true }); + const pageCount = (await gif.metadata()).pages; + if (pageCount == 1) { + data = await sharp(file, { animated: false }) + .gif({ colors: 16 }) + .toBuffer(); + } else { + data = await sharp(file, { animated: true }) + .gif({ colors: 16 }) + .toBuffer(); + } } - if(nonOptimized.byteLength < data.byteLength) { - if(this.logging) { - console.log("❌ " + filePath + "("+nonOptimized.byteLength+"B) optimized was " + data.byteLength + "B. It has been skipped."); + if (nonOptimized.byteLength < data.byteLength) { + if (this.logging) { + console.log( + "❌ " + + filePath + + "(" + + nonOptimized.byteLength + + "B) optimized was " + + data.byteLength + + "B. It has been skipped." + ); } data = nonOptimized; }