-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImageCompress.1.1.js
More file actions
131 lines (121 loc) · 4.36 KB
/
ImageCompress.1.1.js
File metadata and controls
131 lines (121 loc) · 4.36 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
/**
* 前端压缩图像demo
* Created by Sunny An on 2017/1/4.
*/
(function () {
var maxLength = 800; // 最大长度或宽度为800px
var maxNotCompressSize = 200; // 200KB以内的文件不压缩
var quality = 0.8; // 图片压缩质量 0-1之间 1最好
var img = document.createElement('img');
// 解决 Tainted canvases may not be exported 问题
img.setAttribute('crossOrigin', 'Anonymous');
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
/**
* 获得一个默认配置
* @returns {{maxLength: number, maxNotCompressSize: number, quality: number, file: null, success: null, error: null}}
*/
var getOptions = function() {
return {
'maxLength': maxLength, // 最大长度或宽度
'maxNotCompressSize': maxNotCompressSize, // 不压缩指定大小以内的文件 单位 KB
'quality': quality, // 图片压缩质量 0-1之间 1最好
'file': null, // 必须,input中的文件
'success': null, // 必须,成功的回调
'error': null // 失败的回调
};
};
/**
* 压缩图片
* @param option 配置
*/
var compressImage = function (option) {
if (option == null || option.file == null || option.success == null) {
return;
}
if (option.maxLength == null) {
option.maxLength = maxLength;
}
if (option.maxNotCompressSize == null) {
option.maxNotCompressSize = maxNotCompressSize;
}
if (option.quality == null) {
option.quality = quality;
}
// image file -> data URL
var reader = new FileReader();
reader.onload = function (event) {
if (option.file.size / 1024 <= option.maxNotCompressSize) {
option.success(event.target.result);
return;
}
img.src = event.target.result;
};
reader.readAsDataURL(option.file);
// image onload
img.onload = function () {
// 缩放的宽高
var nowWidth = 0;
var nowHeight = 0;
// 图片真实宽高
var realWidth = 0;
var realHeight = 0;
realWidth = img.naturalWidth; // html5 新属性
realHeight = img.naturalHeight;
if (realWidth == 0 || realHeight == 0) {
if(option.error) {
option.error('图片加载失败');
}
return;
}
// 计算缩放后大小
if (realHeight > option.maxLength && realHeight >= realWidth) { // 高度 > 宽度
nowHeight = option.maxLength;
nowWidth = parseInt((nowHeight / realHeight) * realWidth);
} else if (realWidth > option.maxLength && realWidth >= realHeight) { // 宽度 > 高度
nowWidth = option.maxLength;
nowHeight = parseInt((nowWidth / realWidth) * realHeight);
} else {
nowWidth = realWidth;
nowHeight = realHeight;
}
// 压缩图片
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = nowWidth;
canvas.height = nowHeight;
ctx.drawImage(img, 0, 0, nowWidth, nowHeight);
option.success(canvas.toDataURL('image/jpeg', option.quality)); // 取值范围为 0 到 1
};
};
/**
* dataURL 转 Blob
*/
var dataURLtoBlob = function (dataURL) {
var arr = dataURL.split(',');
var mime = arr[0].match(/:(.*?);/)[1];
var decodedData = atob(arr[1]);
var length = decodedData.length;
var u8arr = new Uint8Array(length);
while (length--) {
u8arr[length] = decodedData.charCodeAt(length);
}
return new Blob([u8arr], {type: mime});
};
/**
* 检查文件类型
*/
var checkFileType = function(file) {
if (file == null) {
return false;
}
var patten = /^.+\.(jpg|jpeg|gif|png)$/i;
return file.name.match(patten) != null;
};
// 公开的方法
window.ImageCompress = {
'compressImage': compressImage,
'getOptions': getOptions,
'dataURLtoBlob': dataURLtoBlob,
'checkFileType': checkFileType
};
})();