@selfcommunity/utils
Version:
Utilities to integrate a Community.
47 lines (46 loc) • 1.77 kB
JavaScript
const dataURItoBlob = (dataURI) => {
// convert base64/URLEncoded data component to raw binary data held in a string
let byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
let ia = new Uint8Array(byteString.length);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], { type: mimeString });
};
const resizeImage = (file) => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
let image = new Image();
image.onload = function (imageEvent) {
// Resize the image
let canvas = document.createElement('canvas'), max_size = 1920, width = image.width, height = image.height;
if (width > height) {
if (width > max_size) {
height *= max_size / width;
width = max_size;
}
}
else {
if (height > max_size) {
width *= max_size / height;
height = max_size;
}
}
canvas.width = width;
canvas.height = height;
canvas.getContext('2d').drawImage(image, 0, 0, width, height);
resolve(dataURItoBlob(canvas.toDataURL('image/jpeg')));
};
image.src = event.target.result;
};
reader.onerror = reject;
reader.readAsDataURL(file);
});
export { resizeImage };