JS批量获取图片宽高

JavaScript 获取图片宽高用于表单校验或图片展示

获取单张图片的宽高
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
getImageSize (src) {
return new Promise((resolve, reject) => {
let img = new Image()
img.src = src
if (img.compltet) {
resolve({w: img.width, h: img.height})
} else {
img.onload = function () {
resolve({w: img.width, h: img.height})
}
img.onerror = function (e) {
reject(e)
}
}
})
}
批量获取图片宽高
1
2
3
4
5
6
7
getImgListSize (imgSrcList) {
let arr = []
return Promise.all(imgSrcList.map((item, index) => {
arr.push(null)
return this.getImageSize(item)
}))
}

0%