Skip to content

Commit 2867693

Browse files
committed
bugfixes
1 parent 79d5c50 commit 2867693

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ dist
22
build
33
node_modules
44
.env
5-
.vscode
5+
.vscode
6+
.DS_Store

main.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,10 @@ const compress = async (info: any) => {
691691
const sourceExt = path.extname(info.source).replaceAll(".", "")
692692
const ext = path.extname(dest).replaceAll(".", "")
693693
const resizeCondition = options.keepRatio ? (options.percentage ? options.resizeWidth !== 100 : true) : (options.percentage ? (options.resizeWidth !== 100 && options.resizeHeight !== 100) : true)
694+
let isAnimated = sourceExt === "gif"
695+
if (sourceExt === "webp") {
696+
isAnimated = functions.isAnimatedWebp(buffer)
697+
}
694698
if (ext === "gif") {
695699
if (resizeCondition) {
696700
if (process.platform === "win32") {
@@ -733,12 +737,14 @@ const compress = async (info: any) => {
733737
buffer = await s.toBuffer()
734738
}
735739
if (options.quality !== 100) {
736-
buffer = await imagemin.buffer(buffer, {plugins: [
737-
imageminMozjpeg({quality: options.quality}),
738-
imageminPngquant(),
739-
imageminWebp({quality: options.quality}),
740-
imageminGifsicle({optimizationLevel: 3})
741-
]})
740+
if (!isAnimated && ext !== "avif" && ext !== "jxl") {
741+
buffer = await imagemin.buffer(buffer, {plugins: [
742+
imageminMozjpeg({quality: options.quality}),
743+
imageminPngquant(),
744+
imageminWebp({quality: options.quality}),
745+
imageminGifsicle({optimizationLevel: 3})
746+
]})
747+
}
742748
}
743749
}
744750
fs.writeFileSync(options.overwrite ? info.source : dest, buffer)
@@ -803,6 +809,10 @@ ipcMain.handle("compress-realtime", async (event, info: any) => {
803809
const sourceExt = path.extname(info.source).replaceAll(".", "")
804810
const ext = path.extname(dest).replaceAll(".", "")
805811
const resizeCondition = options.keepRatio ? (options.percentage ? options.resizeWidth !== 100 : true) : (options.percentage ? (options.resizeWidth !== 100 && options.resizeHeight !== 100) : true)
812+
let isAnimated = sourceExt === "gif"
813+
if (sourceExt === "webp") {
814+
isAnimated = functions.isAnimatedWebp(buffer)
815+
}
806816
if (ext === "gif") {
807817
if (resizeCondition) {
808818
if (process.platform === "win32") {
@@ -845,12 +855,14 @@ ipcMain.handle("compress-realtime", async (event, info: any) => {
845855
buffer = await s.toBuffer()
846856
}
847857
if (options.quality !== 100) {
848-
buffer = await imagemin.buffer(buffer, {plugins: [
849-
imageminMozjpeg({quality: options.quality}),
850-
imageminPngquant(),
851-
imageminWebp({quality: options.quality}),
852-
imageminGifsicle({optimizationLevel: 3})
853-
]})
858+
if (!isAnimated && ext !== "avif" && ext !== "jxl") {
859+
buffer = await imagemin.buffer(buffer, {plugins: [
860+
imageminMozjpeg({quality: options.quality}),
861+
imageminPngquant(),
862+
imageminWebp({quality: options.quality}),
863+
imageminGifsicle({optimizationLevel: 3})
864+
]})
865+
}
854866
}
855867
}
856868
return {buffer, fileSize: Buffer.byteLength(buffer)}

structures/functions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,13 @@ export default class Functions {
294294
}
295295
return newLines.join("\n")
296296
}
297+
298+
public static isAnimatedWebp = (buffer: Buffer) => {
299+
let str = buffer.toString("utf-8")
300+
if (str.includes("ANMF")) {
301+
return true
302+
} else {
303+
return false
304+
}
305+
}
297306
}

0 commit comments

Comments
 (0)