-
Notifications
You must be signed in to change notification settings - Fork 15
/
load-image.js
27 lines (22 loc) · 918 Bytes
/
load-image.js
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
export default function loadImg(dataUrl, dims) {
// destructure props from dims
const { minWidth, maxWidth, minHeight, maxHeight } = dims
return new Promise((resolve, reject) => {
// create a new html image element
const img = new Image()
// set the image src attribute to our dataUrl
img.src = dataUrl
// listen for onload event
img.onload = () => {
// validate the min and max image dimensions
if (img.width < minWidth || img.height < minHeight) {
reject(new Error(`The uploaded image is too small. Must be at least ${minWidth}px by ${minHeight}px.`))
}
if (img.width > maxWidth || img.height > maxHeight) {
reject(new Error(`The uploaded image is too large. Must be no more than ${maxWidth}px by ${maxHeight}px.`))
}
resolve(true)
}
img.onerror = () => reject(new Error('There was an error uploading the image'))
})
}