From 381f97ecef0ae304168f9127731ee69f73c27815 Mon Sep 17 00:00:00 2001 From: Alexandre Capt Date: Tue, 24 Jan 2023 17:24:00 +0100 Subject: [PATCH] fix: round image w/h and improve logging (#91) --- modules/importer.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/modules/importer.js b/modules/importer.js index 067f9046..77aa7bca 100644 --- a/modules/importer.js +++ b/modules/importer.js @@ -49,18 +49,25 @@ const options = { } } + width = Math.round(width); + height = Math.round(height); + // note: OffscreenCanvas is not supported on safari const canvas = new OffscreenCanvas(width, height); const ctx = canvas.getContext('2d'); - ctx.drawImage(img, 0, 0); - const newBlob = await canvas.convertToBlob(); - console.log('converted', type, 'to png', src, width, height, blob.size); - return { - data: newBlob.arrayBuffer(), - width, - height, - type: 'image/png', - }; + try { + ctx.drawImage(img, 0, 0); + const newBlob = await canvas.convertToBlob(); + return { + data: newBlob.arrayBuffer(), + width, + height, + type: 'image/png', + }; + } catch (e) { + console.warn(`Cannot convert image ${src} to png. It might corrupt the Word document and you should probably remove it from the DOM.`); + return null; + } }, };