| 23 | } |
| 24 | |
| 25 | export async function fetchImageDimensions( |
| 26 | file: File |
| 27 | ): Promise<{ width: number; height: number }> { |
| 28 | return new Promise((resolve, reject) => { |
| 29 | var url = URL.createObjectURL(file); |
| 30 | var image = new Image(); |
| 31 | |
| 32 | image.onload = function () { |
| 33 | if (!image.width || !image.height) { |
| 34 | return reject(new Error(`Image is empty ${file.name}`)); |
| 35 | } |
| 36 | resolve({ |
| 37 | width: image.width, |
| 38 | height: image.height, |
| 39 | }); |
| 40 | URL.revokeObjectURL(image.src); |
| 41 | }; |
| 42 | |
| 43 | image.onerror = function () { |
| 44 | reject(new Error(`Couldn't load the image ${file.name}`)); |
| 45 | }; |
| 46 | |
| 47 | image.src = url; |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | interface Dimensions { |
| 52 | width: number; |