-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
108 lines (98 loc) · 3.21 KB
/
index.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
export class Create {
constructor(options = {}) {
const defaults = {
mimeType: 'image/png',
quality: 1
};
this.canvas = null;
this.options = Object.assign(defaults, options);
}
compress(file, options = {}) {
if (!file) {
return;
}
const params = Object.assign(this.options, options);
let image;
this.compressImage(file, params)
.then((img) => {
image = img;
return this.toBlob(img, params);
})
.then((blob) => {
if (window.URL) {
window.URL.revokeObjectURL(image.src);
}
blob.name = file.name;
if (params.success) {
params.success.call(this, blob);
}
})
.catch((err) => {
if (!params.error) {
throw err;
}
params.error.call(this, err);
});
}
compressImage(file) {
return new Promise((resolve, reject) => {
const image = new Image();
image.onload = () => resolve(image);
if (window.URL) {
image.src = window.URL.createObjectURL(file);
} else {
const reader = new FileReader();
reader.onload = (e) => {
image.src = e.target.result;
};
reader.onabort = () => {
reject(new Error('Aborted to load the image with FileReader.'));
};
reader.onerror = () => {
reject(new Error('Failed to load the image with FileReader.'));
};
reader.readAsDataURL(file);
}
});
}
toBlob(image, options) {
const rect = this.getRect(image, options);
return new Promise((resolve, reject) => {
if (!this.canvas) {
this.canvas = document.createElement('canvas');
}
if (!this.canvas.getContext) {
reject('Unsupport Canvas!');
} else {
const ctx = this.canvas.getContext('2d');
this.canvas.width = rect.width;
this.canvas.height = rect.height;
ctx.drawImage(image, 0, 0, rect.width, rect.height);
const done = (blob) => {
if (blob) {
resolve(blob);
}
};
this.canvas.toBlob(done, options.mimeType, options.quality);
}
});
}
getRect(image, options) {
const naturalWidth = image.naturalWidth;
const naturalHeight = image.naturalHeight;
const aspectRatio = naturalWidth / naturalHeight;
let width = naturalWidth;
let height = naturalHeight;
if (options.width > 0) {
width = options.width;
height = options.width / aspectRatio;
} else if (options.height > 0) {
height = options.height;
width = height * aspectRatio;
}
return {
width,
height
};
}
}