File size: 927 Bytes
d5488ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
async function resize(image_file,max_size=1000){
    // Load the image
    // Get as image data
    const imageBitmap = await createImageBitmap(image_file);
    
    // Resize the image
    var canvas = document.createElement('canvas'),
    
    width = imageBitmap.width,
    height = imageBitmap.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(imageBitmap, 0, 0, width, height);
    
    const blob = await new Promise((resolve) =>
            canvas.toBlob(resolve, 'image/jpeg')
        );

    // Turn Blob into File
    return new File([blob], image_file.name, {
        type: blob.type,
    });
}