78 lines
4.8 KiB
JavaScript
78 lines
4.8 KiB
JavaScript
function asyncCall(fileList, loopFiles, da_width, da_height) {
|
|
const myPromise = new Promise(compress(fileList, loopFiles, da_width, da_height)).then((response) => {
|
|
validate(response, loopFiles);
|
|
});
|
|
}
|
|
|
|
function compress(initFileList, lf, maxWidth, maxHeight) {
|
|
return async function(resolve, reject) {
|
|
try {
|
|
var compressedFileList = [];
|
|
for (var file = 0; file < initFileList.length; file++) {
|
|
if (initFileList[file].name.match(/\.jpg$/i) || initFileList[file].name.match(/\.jpeg$/i)) {
|
|
var canvas = document.createElement('canvas');
|
|
const ctx = canvas.getContext('2d');
|
|
const img = await createImageBitmap(initFileList[file]);
|
|
// calculate new size
|
|
if (img.width > maxWidth && img.height > maxHeight) { //threshold for image resizeability
|
|
if (document.getElementById("file-size-" + initFileList[file].name) !== null) {
|
|
if (document.getElementById("file-size-" + initFileList[file].name).style.visibility = "visible") {
|
|
if (document.getElementById("file-original-size-" + initFileList[file].name.replace(".", "_")).checked) {
|
|
var width = img.width;
|
|
var height = img.height;
|
|
// resize the canvas to the new dimensions
|
|
canvas.width = width;
|
|
canvas.height = height;
|
|
// scale & draw the image onto the canvas
|
|
ctx.drawImage(img, 0, 0, width, height);
|
|
// Get the binary (aka blob)
|
|
var blob = await new Promise(rs => canvas.toBlob(rs, "image/jpeg", 1));
|
|
var resizedFile = new File([blob], initFileList[file].name, initFileList[file]);
|
|
compressedFileList[file] = resizedFile;
|
|
} else if (document.getElementById("file-normal-size-" + initFileList[file].name.replace(".", "_")).checked) {
|
|
var ratio = Math.min(maxWidth / img.width, maxHeight / img.height);
|
|
var width = img.width * ratio + .5 | 0;
|
|
var height = img.height * ratio + .5 | 0;
|
|
// resize the canvas to the new dimensions
|
|
canvas.width = width;
|
|
canvas.height = height;
|
|
// scale & draw the image onto the canvas
|
|
ctx.drawImage(img, 0, 0, width, height);
|
|
// Get the binary (aka blob)
|
|
var blob = await new Promise(rs => canvas.toBlob(rs, "image/jpeg", 1));
|
|
var resizedFile = new File([blob], initFileList[file].name, initFileList[file]);
|
|
compressedFileList[file] = resizedFile;
|
|
} else {
|
|
console.log("Unknown safe state BijlagenForm_async.js");
|
|
}
|
|
}
|
|
} else {
|
|
//current save state: normal default
|
|
var ratio = Math.min(maxWidth / img.width, maxHeight / img.height);
|
|
var width = img.width * ratio + .5 | 0;
|
|
var height = img.height * ratio + .5 | 0;
|
|
// resize the canvas to the new dimensions
|
|
canvas.width = width;
|
|
canvas.height = height;
|
|
// scale & draw the image onto the canvas
|
|
ctx.drawImage(img, 0, 0, width, height);
|
|
// Get the binary (aka blob)
|
|
var blob = await new Promise(rs => canvas.toBlob(rs, "image/jpeg", 1));
|
|
var resizedFile = new File([blob], initFileList[file].name, initFileList[file]);
|
|
compressedFileList[file] = resizedFile;
|
|
}
|
|
} else {
|
|
compressedFileList[file] = initFileList[file];
|
|
}
|
|
|
|
} else {
|
|
compressedFileList[file] = initFileList[file];
|
|
}
|
|
}
|
|
resolve(compressedFileList);
|
|
} catch(error){
|
|
console.log("Error: File might be corrupted.");
|
|
console.log(error);
|
|
}
|
|
}
|
|
} |