-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg-compress.js
190 lines (168 loc) · 5.22 KB
/
img-compress.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
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
186
class Compress {
constructor(file, opts) {
if (!window.FileReader || !window.Blob) {
return console.warn('您的浏览器不支持图片压缩');
}
let config = Object.assign({
file: file,
reader: new FileReader(),
quality: 0.5, // 压缩系数
mimeType: file.type, // 读取文件类型
imageInfo: null, // 保存图片信息,包括处理前和处理后
orientation: 1, // 图片方向
}, opts);
for (let key in config) {
this[key] = config[key];
}
}
init() {
if (typeof this.file === 'string') {
let arrType = this.file.split('.');
this.mimeType =arrType[arrType.length - 1];
return this.initWithImagePath();
} else {
return this.initWithImageFile();
}
}
// 图片地址初始化
initWithImagePath() {
let self = this,
img = new Image(),
canvas = document.createElement('canvas'),
ctx = null,
imgCompressData = undefined,
blobData = undefined;
img.crossOrigin = "Anonymous"; // 跨域加载
let promise = new Promise((resolve, reject) => {
img.onload = function() {
/**
* this 为 img
* self 为 class
*/
canvas.width = this.naturalWidth;
canvas.height = this.naturalHeight;
ctx = canvas.getContext('2d');
ctx.drawImage(this, 0, 0);
imgCompressData = canvas.toDataURL(self.mimeType, self.quality);
blobData = self.dataURIToBlob(imgCompressData);
self.ctx = ctx;
self.canvas = canvas;
self.imageInfo = {
naturalWidth: this.naturalWidth,
naturalHeight: this.naturalHeight,
base64: imgCompressData,
blobData: blobData
}
resolve(self);
}
img.onerror = (res) => {
reject(res);
}
});
img.src = this.file;
return promise;
}
// 图片文件初始化
initWithImageFile() {
let self = this,
img = new Image(),
promise = new Promise((resolve, reject) => {
this.reader.onload = function() {
let dataURL = this.result;
img.onload = function() {
/**
* this 为 img
* self 为 class
*/
let canvas = document.createElement('canvas'),
ctx = null,
imgCompressData = undefined,
blobData = undefined,
width = this.naturalWidth,
height = this.naturalHeight;
ctx = canvas.getContext('2d');
ctx.save();
switch (self.orientation) {
case 1:
canvas.width = width;
canvas.height = height;
ctx.drawImage(this, 0, 0);
break;
case 6:
canvas.width = height;
canvas.height = width;
ctx.rotate(90 * Math.PI / 180);
ctx.drawImage(this, 0, -height);
break;
case 8:
canvas.width = width;
canvas.height = height;
ctx.rotate( -90 * Math.PI / 180);
ctx.drawImage(this, -width, -height);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(2 * 90 * Math.PI / 180);
ctx.drawImage(this, -width, 0);
break;
default:
canvas.width = width;
canvas.height = height;
ctx.drawImage(this, 0, 0);
}
ctx.restore();
imgCompressData = canvas.toDataURL(self.mimeType, self.quality);
blobData = self.dataURIToBlob(this.getAttribute('src'));
self.ctx = ctx;
self.canvas = canvas;
self.imageInfo = {
naturalWidth: canvas.width,
naturalHeight: canvas.height,
base64: imgCompressData,
blobData: blobData
}
resolve(self);
}
img.onerror = function(res) {
reject(res);
}
img.src = dataURL;
}
this.reader.onerror = (res) => {
console.warn(res);
}
this.reader.readAsDataURL(this.file);
});
return promise;
}
dataURIToBlob(dataURI) {
let byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
byteString = atob(dataURI.split(',')[1]);
} else {
byteString = unescape(dataURI.split(',')[1]);
}
let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0],
ia = new Uint8Array(byteString.length);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
clip(left, top, width, heigh, quality = 0.5) {
let data = this.ctx.getImageData(left, top, width, heigh),
canvas = document.createElement('canvas'),
ctx,
dataURI,
result;
canvas.width = width;
canvas.height = heigh;
ctx = canvas.getContext('2d');
ctx.putImageData(data, 0, 0);
dataURI = canvas.toDataURL(this.mimeType, quality);
result = this.dataURIToBlob(dataURI);
return result;
}
}
module.exports = Compress;