Commit 49df378a authored by dtati's avatar dtati

rezise image

parent 95833b6d
...@@ -23,13 +23,13 @@ export default { ...@@ -23,13 +23,13 @@ export default {
this.readImage(selectedFile); this.readImage(selectedFile);
} }
}, },
readImage(file) { async readImage(file) {
const reader = new FileReader(); const reader = new FileReader();
reader.onload = (e) => { reader.onload = (e) => {
this.imagePreview = e.target.result; this.imagePreview = e.target.result;
}; };
reader.readAsDataURL(file); reader.readAsDataURL(file);
this.binaryFile = file; this.binaryFile = await Helper.resizeImage(file)
this.init() this.init()
}, },
startScanFace(){ startScanFace(){
......
...@@ -29,6 +29,51 @@ export default { ...@@ -29,6 +29,51 @@ export default {
bytes[i] = binary_string.charCodeAt(i); bytes[i] = binary_string.charCodeAt(i);
} }
return bytes.buffer; return bytes.buffer;
},
resizeImage (file) {
console.log(file);
const defaultQualityRatio = 0.9;
const imageType = file.type || 'image/jpeg';
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (e) => {
const img = new Image();
img.src = e.target.result;
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 1000;
canvas.height = 600;
if (img.width < img.height) {
canvas.width = 600;
canvas.height = 1000;
}
if (img.width > canvas.width || img.height > canvas.height) {
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
ctx.canvas.toBlob(
(blob) => {
resolve(
new File([blob], file.name, {
type: imageType,
lastModified: Date.now(),
})
);
},
imageType,
defaultQualityRatio
);
}
};
};
reader.onerror = (error) => {reject(error);};
});
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment