-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageProcess.ts
More file actions
45 lines (38 loc) · 1.32 KB
/
imageProcess.ts
File metadata and controls
45 lines (38 loc) · 1.32 KB
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
36
37
38
39
40
41
42
43
44
45
import { ElUploadInternalRawFile } from 'element-ui/types/upload'
import { compress, compressAccurately, ICompressConfig, compressAccuratelyConfig } from 'image-conversion'
// 图片压缩
export async function compressImgRaw (
imgRaw: ElUploadInternalRawFile,
config: ICompressConfig | compressAccuratelyConfig | number
) {
const compressMethod = ((typeof config === 'number' && config > 1) ||
(config as compressAccuratelyConfig).size ||
(config as compressAccuratelyConfig).accuracy)
? compressAccurately : compress
const compressedImgBlob = await compressMethod(imgRaw, config)
const compressedFile = new File(
[compressedImgBlob],
imgRaw.name,
{ type: compressedImgBlob.type }
) as ElUploadInternalRawFile
return compressedFile
}
/**
* 获取图片文件的元素信息,比如宽、高等
* -------------------- */
export function getImgElementInfoByFileRaw (imgFileRaw: ElUploadInternalRawFile): Promise<null | HTMLImageElement> {
return new Promise((resolve) => {
if (!imgFileRaw.type.startsWith('image')) {
resolve(null)
return
}
const url = URL.createObjectURL(imgFileRaw)
const img = document.createElement('img')
img.onload = () => {
resolve(img)
URL.revokeObjectURL(url)
}
img.onerror = () => resolve(null)
img.src = url
})
}