From f147933701073d0706bfedf20a120d989969a16c Mon Sep 17 00:00:00 2001 From: john Date: Mon, 26 May 2025 22:17:49 +0200 Subject: [PATCH] make optimization slightly less wonky? --- src/components/NewPostWidget.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/components/NewPostWidget.tsx b/src/components/NewPostWidget.tsx index 0dd8968..7146bc7 100644 --- a/src/components/NewPostWidget.tsx +++ b/src/components/NewPostWidget.tsx @@ -185,6 +185,7 @@ async function optimizeImageSize( const scale = Math.min(1, targetMaxWidth / img.width, targetMaxHeight / img.height) const width = Math.floor(img.width * scale) const height = Math.floor(img.height * scale) + const originalSize = file.size const srcCanvas = document.createElement('canvas') srcCanvas.width = img.width @@ -200,10 +201,14 @@ async function optimizeImageSize( let blob = await pica.toBlob(dstCanvas, outputType, quality) while (blob.size > targetSizeBytes && quality > 0.1) { - quality = parseFloat((quality - 0.1).toFixed(2)) + quality -= 0.1 blob = await pica.toBlob(dstCanvas, outputType, quality) } + console.debug( + `optimized image rendered at ${Math.round(quality * 100)}% quality to ${blob.size / 1000}KB from ${originalSize / 1000}KB`, + ) + URL.revokeObjectURL(url) return new File([blob], file.name, { type: file.type })