-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
185 lines (183 loc) · 5.91 KB
/
index.js
File metadata and controls
185 lines (183 loc) · 5.91 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* base64 to blob
*
* @export
* @param {string} urlData
* @returns {Blob}
*/
export function base64ToBlob(urlData){
var arr = urlData.split(',');
var mime = arr[0].match(/:(.*?);/)[1] || 'image/png';
var bytes = window.atob(arr[1]);
var ab = new ArrayBuffer(bytes.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < bytes.length; i++) {
ia[i] = bytes.charCodeAt(i);
}
return new Blob([ab], {
type: mime
});
}
/**
* img file to base64
*
* @export
* @param {*} file input's select file
* @returns Promise
*/
export function imgFileToBase64(file){
return new Promise((resolve,reject) => {
try {
let reader = new FileReader();
reader.onload = e => {
let base64Img = e.target.result;
resolve(base64Img);
}
reader.readAsDataURL(file);
} catch (err) {
reject(err);
}
})
}
/**
* base64 to file
* @param dataurl base64 Code
* @param filename file name
* @returns {File}
*/
export function base64ToFile(dataurl, filename){
let arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}
/**
*
*
* @export
* @param mode Picture compression mode:auto(Compression in equal proportion to maximum width or height) width(Compression by width) height(Compression by height)
* @param dataSource Data source, passing:image/base64/canvas/img file
* @param dataSourceType Data source type: image/base64/canvas/file
* @param maxWidth Maximum width of compression,default: 1080
* @param maxHeight Maximum height of compression,default: 1080
* @param quality Picture output quality;Range of values: 0-1 ;default: 0.92;It's useing canvas function:toDataUrl;
* @returns Promise
*/
export function imgCompress({mode="auto",dataSource,dataSourceType,maxWidth=0,maxHeight=0,quality=0.92}){
/**
* draw canvas
*
* @param {*} img
* @param {*} offsetWidth
* @param {*} offsetHeight
* @param {*} realWidth
* @param {*} realHeight
* @returns
*/
const _drawToCanvas = (img,offsetWidth,offsetHeight,realWidth,realHeight) => {
return new Promise((resolve,reject) => {
try {
const canvas = document.createElement("canvas");
canvas.width = offsetWidth;
canvas.height = offsetHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(
img,
0,0,
realWidth,
realHeight,
0,0,
offsetWidth,
offsetHeight
);
const base64str = canvas.toDataURL("image/jpeg",quality);
resolve(base64str);
} catch (err) {
reject(err);
}
})
}
/**
* check img compress size
*
* @param {*} img
* @returns
*/
const _getResizeSizeImg = (img) => {
const originImgWidth = img.width;
const originImgHeight = img.height;
const percentScale = parseFloat(originImgWidth / originImgHeight);
if(originImgWidth <= 1080 && originImgHeight <= 1080){
return {width:originImgWidth , height:originImgHeight};
}
if(mode == "auto"){
let autoWidth = maxWidth == 0 ? 1080 : maxWidth;
let autoHeight = maxHeight == 0 ? 1080 : maxHeight;
let sizeByMaxWidth = {
width: autoWidth,
height: parseInt(autoWidth / percentScale)
};
let sizeByMaxHeight = {
width: parseInt(autoHeight / percentScale),
height: autoHeight,
};
if(sizeByMaxHeight.height <= maxWidth){
return sizeByMaxHeight
}
return sizeByMaxWidth
}else if(mode == "width"){
if(originImgWidth <= maxWidth){
return { originImgWidth , originImgHeight };
}
let autoWidth = maxWidth == 0 ? 1080 : maxWidth;
let sizeByMaxWidth = {
width: autoWidth,
height: parseInt(autoWidth / percentScale)
};
return sizeByMaxWidth;
}else {
if(originImgHeight <= maxHeight){
return { originImgWidth , originImgHeight };
}
let autoWidth = maxHeight == 0 ? 1080 : maxHeight;
let sizeByMaxHeight = {
width: parseInt(autoWidth / percentScale),
height: maxHeight,
};
return sizeByMaxHeight
}
}
return new Promise((resolve,reject) => {
let originImage = new Image();
originImage.onload = () => {
const finalSize = _getResizeSizeImg(originImage);
_drawToCanvas(originImage,finalSize.width,finalSize.height,originImage.width,originImage.height).then(res => {
resolve(res)
}).catch((err) => {
reject(err)
});
};
// switch all external incoming types to Base64
if(dataSourceType == "img" || dataSourceType == "image"){
originImage.src = dataSource.src;
}else if(dataSourceType == "base64"){
originImage.src = dataSource;
}else if(dataSourceType == "canvas"){
originImage.src = dataSource.toDataUrl("image/jpeg");
}else if(dataSourceType == "file"){
imgFileToBase64(dataSource).then(res => {
originImage.src = res;
}).catch(err => {
throw err
});
}else{
console.log('unknow type:'+dataSourceType)
reject('unknow type:'+dataSourceType)
}
})
}