Skip to content

Commit 67b80f7

Browse files
committed
feat: increase initial PNG color depth to improve output quality #180
1 parent 1611973 commit 67b80f7

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/image-compression.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ export default async function compress(file, options, previousProgress = 0) {
112112

113113
ctx.drawImage(canvas, 0, 0, newWidth, newHeight);
114114

115-
quality *= 0.95;
115+
if (outputFileType === 'image/png') {
116+
quality *= 0.85;
117+
} else {
118+
quality *= 0.95;
119+
}
116120
// eslint-disable-next-line no-await-in-loop
117121
compressedFile = await canvasToFile(newCanvas, outputFileType, file.name, file.lastModified, quality);
118122

lib/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ export async function canvasToFile(canvas, fileType, fileName, fileLastModified,
257257
if (fileType === 'image/png') {
258258
const ctx = canvas.getContext('2d');
259259
const { data } = ctx.getImageData(0, 0, canvas.width, canvas.height);
260-
const png = UPNG.encode([data], canvas.width, canvas.height, 256 * quality);
260+
if (process.env.BUILD === 'development') {
261+
console.log('png no. of colors', 4096 * quality);
262+
}
263+
const png = UPNG.encode([data.buffer], canvas.width, canvas.height, 4096 * quality);
261264
file = new Blob([png], { type: fileType });
262265
file.name = fileName;
263266
file.lastModified = fileLastModified;

0 commit comments

Comments
 (0)