i try to resize base64 image But I did not find an answer. I tried canvas but did not answer. I do not know why ... This was the code
const canvas = document.createElement('canvas'),
ctx = <CanvasRenderingContext2D>canvas.getContext('2d');
canvas.width = 100;
canvas.height = 100;
const img = new Image();
img.src = this.SelectedFile.src;
ctx.drawImage(img , 0, 0, 100, 100);
this.SelectedFile.src = ctx.canvas.toDataURL();
Does anyone know any other way or knows what the problem is?
Add this helper function outside of the given component:
function compressImage(src, newX, newY) {
return new Promise((res, rej) => {
const img = new Image();
img.src = src;
img.onload = () => {
const elem = document.createElement('canvas');
elem.width = newX;
elem.height = newY;
const ctx = elem.getContext('2d');
ctx.drawImage(img, 0, 0, newX, newY);
const data = ctx.canvas.toDataURL();
res(data);
}
img.onerror = error => rej(error);
})
}
Then call like so:
compressImage(base64, 100, 100).then(compressed => {
this.resizedBase64 = compressed;
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With