Spaces:
Running
Running
File size: 1,001 Bytes
f2bee8a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
export default (bmpImage, type = 'image/bmp') => new Promise(resolve => {
// If the input is an ArrayBuffer, we need to convert it to a `Blob` and give it a URL so we can use it as an <img>
// `src`. If it's a data URI, we can use it as-is.
const imageUrl = bmpImage instanceof String ?
bmpImage :
window.URL.createObjectURL(new Blob([bmpImage], {type}));
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const image = document.createElement('img');
image.addEventListener('load', () => {
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
ctx.drawImage(image, 0, 0);
const dataUrl = canvas.toDataURL('image/png');
// Revoke URL. If a blob URL was generated earlier, this allows the blob to be GC'd and prevents a memory leak.
window.URL.revokeObjectURL(imageUrl);
resolve(dataUrl);
});
image.setAttribute('src', imageUrl);
});
|