Skip to content

Commit

Permalink
fix: round image w/h and improve logging (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe authored Jan 24, 2023
1 parent 05a39d2 commit 381f97e
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions modules/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
};

Expand Down

0 comments on commit 381f97e

Please sign in to comment.