Spaces:
Build error
Build error
Commit
·
d5488ab
1
Parent(s):
07d8486
added resize.js
Browse files- app/static/resize.js +34 -0
app/static/resize.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
async function resize(image_file,max_size=1000){
|
| 2 |
+
// Load the image
|
| 3 |
+
// Get as image data
|
| 4 |
+
const imageBitmap = await createImageBitmap(image_file);
|
| 5 |
+
|
| 6 |
+
// Resize the image
|
| 7 |
+
var canvas = document.createElement('canvas'),
|
| 8 |
+
|
| 9 |
+
width = imageBitmap.width,
|
| 10 |
+
height = imageBitmap.height;
|
| 11 |
+
if (width > height) {
|
| 12 |
+
if (width > max_size) {
|
| 13 |
+
height *= max_size / width;
|
| 14 |
+
width = max_size;
|
| 15 |
+
}
|
| 16 |
+
} else {
|
| 17 |
+
if (height > max_size) {
|
| 18 |
+
width *= max_size / height;
|
| 19 |
+
height = max_size;
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
canvas.width = width;
|
| 23 |
+
canvas.height = height;
|
| 24 |
+
canvas.getContext('2d').drawImage(imageBitmap, 0, 0, width, height);
|
| 25 |
+
|
| 26 |
+
const blob = await new Promise((resolve) =>
|
| 27 |
+
canvas.toBlob(resolve, 'image/jpeg')
|
| 28 |
+
);
|
| 29 |
+
|
| 30 |
+
// Turn Blob into File
|
| 31 |
+
return new File([blob], image_file.name, {
|
| 32 |
+
type: blob.type,
|
| 33 |
+
});
|
| 34 |
+
}
|