1200 byte savings in some cases with gifs

This commit is contained in:
Soph :3 2024-09-19 01:41:53 +03:00
parent 7b04c35c94
commit e68ad369e9
Signed by: sophie
GPG key ID: EDA5D222A0C270F2

View file

@ -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;
}