37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
![]() |
export function resizeBase64Image(base64Image) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
const maxSizeInKB = 500;
|
||
|
const maxSizeInBytes = maxSizeInKB * 1024;
|
||
|
const img = new Image();
|
||
|
img.src = base64Image;
|
||
|
img.onload = function () {
|
||
|
const canvas = document.createElement("canvas");
|
||
|
const ctx = canvas.getContext('2d');
|
||
|
const width = img.width;
|
||
|
const height = img.height;
|
||
|
const aspectRatio = width / height;
|
||
|
const newWidth = Math.sqrt(maxSizeInBytes * aspectRatio);
|
||
|
const newHeight = Math.sqrt(maxSizeInBytes / aspectRatio);
|
||
|
canvas.width = newWidth;
|
||
|
canvas.height = newHeight;
|
||
|
ctx.drawImage(img, 0, 0, newWidth, newHeight);
|
||
|
let quality = 0.8;
|
||
|
let dataURL = canvas.toDataURL('image/jpeg', quality);
|
||
|
resolve(dataURL);
|
||
|
};
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export function getBase64OfImage(file, cb) {
|
||
|
let reader = new FileReader();
|
||
|
reader.readAsDataURL(file);
|
||
|
reader.onload = function () {
|
||
|
cb(reader.result)
|
||
|
};
|
||
|
reader.onerror = function (error) {
|
||
|
console.log('Error: ', error);
|
||
|
};
|
||
|
}
|
||
|
|
||
|
|