|
@@ -0,0 +1,222 @@
|
|
1
|
+// clone https://github.com/Tencent/weui.js/blob/master/src/uploader/image.js
|
|
2
|
+// updated by cube-ui
|
|
3
|
+
|
|
4
|
+/*
|
|
5
|
+* Tencent is pleased to support the open source community by making WeUI.js available.
|
|
6
|
+*
|
|
7
|
+* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
|
|
8
|
+*
|
|
9
|
+* Licensed under the MIT License (the "License"); you may not use this file except in compliance
|
|
10
|
+* with the License. You may obtain a copy of the License at
|
|
11
|
+*
|
|
12
|
+* http://opensource.org/licenses/MIT
|
|
13
|
+*
|
|
14
|
+* Unless required by applicable law or agreed to in writing, software distributed under the License is
|
|
15
|
+* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
16
|
+* either express or implied. See the License for the specific language governing permissions and
|
|
17
|
+* limitations under the License.
|
|
18
|
+*/
|
|
19
|
+
|
|
20
|
+/**
|
|
21
|
+ * 检查图片是否有被压扁,如果有,返回比率
|
|
22
|
+ * ref to http://stackoverflow.com/questions/11929099/html5-canvas-drawimage-ratio-bug-ios
|
|
23
|
+ */
|
|
24
|
+function detectVerticalSquash(img) {
|
|
25
|
+ // 拍照在IOS7或以下的机型会出现照片被压扁的bug
|
|
26
|
+ var data;
|
|
27
|
+ var ih = img.naturalHeight;
|
|
28
|
+ var canvas = document.createElement('canvas');
|
|
29
|
+ canvas.width = 1;
|
|
30
|
+ canvas.height = ih;
|
|
31
|
+ var ctx = canvas.getContext('2d');
|
|
32
|
+ ctx.drawImage(img, 0, 0);
|
|
33
|
+ try {
|
|
34
|
+ data = ctx.getImageData(0, 0, 1, ih).data;
|
|
35
|
+ } catch (err) {
|
|
36
|
+ console.log('Cannot check verticalSquash: CORS?');
|
|
37
|
+ return 1;
|
|
38
|
+ }
|
|
39
|
+ var sy = 0;
|
|
40
|
+ var ey = ih;
|
|
41
|
+ var py = ih;
|
|
42
|
+ while (py > sy) {
|
|
43
|
+ var alpha = data[(py - 1) * 4 + 3];
|
|
44
|
+ if (alpha === 0) {
|
|
45
|
+ ey = py;
|
|
46
|
+ } else {
|
|
47
|
+ sy = py;
|
|
48
|
+ }
|
|
49
|
+ py = (ey + sy) >> 1; // py = parseInt((ey + sy) / 2)
|
|
50
|
+ }
|
|
51
|
+ var ratio = (py / ih);
|
|
52
|
+ return (ratio === 0) ? 1 : ratio;
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+/**
|
|
56
|
+* dataURI to blob, ref to https://gist.github.com/fupslot/5015897
|
|
57
|
+* @param dataURI
|
|
58
|
+*/
|
|
59
|
+function dataURItoBuffer(dataURI){
|
|
60
|
+ var byteString = atob(dataURI.split(',')[1]);
|
|
61
|
+ var buffer = new ArrayBuffer(byteString.length);
|
|
62
|
+ var view = new Uint8Array(buffer);
|
|
63
|
+ for (var i = 0; i < byteString.length; i++) {
|
|
64
|
+ view[i] = byteString.charCodeAt(i);
|
|
65
|
+ }
|
|
66
|
+ return buffer;
|
|
67
|
+}
|
|
68
|
+function dataURItoBlob(dataURI) {
|
|
69
|
+ var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
|
|
70
|
+ var buffer = dataURItoBuffer(dataURI);
|
|
71
|
+ return new Blob([buffer], {type: mimeString});
|
|
72
|
+}
|
|
73
|
+
|
|
74
|
+/**
|
|
75
|
+* 获取图片的orientation
|
|
76
|
+* ref to http://stackoverflow.com/questions/7584794/accessing-jpeg-exif-rotation-data-in-javascript-on-the-client-side
|
|
77
|
+*/
|
|
78
|
+function getOrientation(buffer){
|
|
79
|
+ var view = new DataView(buffer);
|
|
80
|
+ if (view.getUint16(0, false) != 0xFFD8) return -2;
|
|
81
|
+ var length = view.byteLength, offset = 2;
|
|
82
|
+ while (offset < length) {
|
|
83
|
+ var marker = view.getUint16(offset, false);
|
|
84
|
+ offset += 2;
|
|
85
|
+ if (marker == 0xFFE1) {
|
|
86
|
+ if (view.getUint32(offset += 2, false) != 0x45786966) return -1;
|
|
87
|
+ var little = view.getUint16(offset += 6, false) == 0x4949;
|
|
88
|
+ offset += view.getUint32(offset + 4, little);
|
|
89
|
+ var tags = view.getUint16(offset, little);
|
|
90
|
+ offset += 2;
|
|
91
|
+ for (var i = 0; i < tags; i++)
|
|
92
|
+ if (view.getUint16(offset + (i * 12), little) == 0x0112)
|
|
93
|
+ return view.getUint16(offset + (i * 12) + 8, little);
|
|
94
|
+ }
|
|
95
|
+ else if ((marker & 0xFF00) != 0xFF00) break;
|
|
96
|
+ else offset += view.getUint16(offset, false);
|
|
97
|
+ }
|
|
98
|
+ return -1;
|
|
99
|
+}
|
|
100
|
+
|
|
101
|
+/**
|
|
102
|
+* 修正拍照时图片的方向
|
|
103
|
+* ref to http://stackoverflow.com/questions/19463126/how-to-draw-photo-with-correct-orientation-in-canvas-after-capture-photo-by-usin
|
|
104
|
+*/
|
|
105
|
+function orientationHelper(canvas, ctx, orientation) {
|
|
106
|
+ const w = canvas.width, h = canvas.height;
|
|
107
|
+ if(orientation > 4){
|
|
108
|
+ canvas.width = h;
|
|
109
|
+ canvas.height = w;
|
|
110
|
+ }
|
|
111
|
+ switch (orientation) {
|
|
112
|
+ case 2:
|
|
113
|
+ ctx.translate(w, 0);
|
|
114
|
+ ctx.scale(-1, 1);
|
|
115
|
+ break;
|
|
116
|
+ case 3:
|
|
117
|
+ ctx.translate(w, h);
|
|
118
|
+ ctx.rotate(Math.PI);
|
|
119
|
+ break;
|
|
120
|
+ case 4:
|
|
121
|
+ ctx.translate(0, h);
|
|
122
|
+ ctx.scale(1, -1);
|
|
123
|
+ break;
|
|
124
|
+ case 5:
|
|
125
|
+ ctx.rotate(0.5 * Math.PI);
|
|
126
|
+ ctx.scale(1, -1);
|
|
127
|
+ break;
|
|
128
|
+ case 6:
|
|
129
|
+ ctx.rotate(0.5 * Math.PI);
|
|
130
|
+ ctx.translate(0, -h);
|
|
131
|
+ break;
|
|
132
|
+ case 7:
|
|
133
|
+ ctx.rotate(0.5 * Math.PI);
|
|
134
|
+ ctx.translate(w, -h);
|
|
135
|
+ ctx.scale(-1, 1);
|
|
136
|
+ break;
|
|
137
|
+ case 8:
|
|
138
|
+ ctx.rotate(-0.5 * Math.PI);
|
|
139
|
+ ctx.translate(-w, 0);
|
|
140
|
+ break;
|
|
141
|
+ }
|
|
142
|
+}
|
|
143
|
+
|
|
144
|
+/**
|
|
145
|
+* 压缩图片
|
|
146
|
+*/
|
|
147
|
+function compress(file, options, callback) {
|
|
148
|
+ const reader = new FileReader();
|
|
149
|
+ reader.onload = function (evt) {
|
|
150
|
+ if(options.compress === false){
|
|
151
|
+ // 不启用压缩 & base64上传 的分支,不做任何处理,直接返回文件的base64编码
|
|
152
|
+ file.base64 = evt.target.result;
|
|
153
|
+ callback(file);
|
|
154
|
+ return;
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ // 启用压缩的分支
|
|
158
|
+ const img = new Image();
|
|
159
|
+ img.onload = function () {
|
|
160
|
+ const ratio = detectVerticalSquash(img);
|
|
161
|
+ const orientation = getOrientation(dataURItoBuffer(img.src));
|
|
162
|
+ const canvas = document.createElement('canvas');
|
|
163
|
+ const ctx = canvas.getContext('2d');
|
|
164
|
+
|
|
165
|
+ const maxW = options.compress.width;
|
|
166
|
+ const maxH = options.compress.height;
|
|
167
|
+ let w = img.width;
|
|
168
|
+ let h = img.height;
|
|
169
|
+ let dataURL;
|
|
170
|
+
|
|
171
|
+ if(w < h && h > maxH){
|
|
172
|
+ w = parseInt(maxH * img.width / img.height);
|
|
173
|
+ h = maxH;
|
|
174
|
+ }else if(w >= h && w > maxW){
|
|
175
|
+ h = parseInt(maxW * img.height / img.width);
|
|
176
|
+ w = maxW;
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ canvas.width = w;
|
|
180
|
+ canvas.height = h;
|
|
181
|
+
|
|
182
|
+ if(orientation > 0){
|
|
183
|
+ orientationHelper(canvas, ctx, orientation);
|
|
184
|
+ }
|
|
185
|
+ ctx.drawImage(img, 0, 0, w, h / ratio);
|
|
186
|
+
|
|
187
|
+ if(/image\/jpeg/.test(file.type) || /image\/jpg/.test(file.type)){
|
|
188
|
+ dataURL = canvas.toDataURL('image/jpeg', options.compress.quality);
|
|
189
|
+ }else{
|
|
190
|
+ dataURL = canvas.toDataURL(file.type);
|
|
191
|
+ }
|
|
192
|
+
|
|
193
|
+ if(options.type == 'file'){
|
|
194
|
+ if(/;base64,null/.test(dataURL) || /;base64,$/.test(dataURL)){
|
|
195
|
+ // 压缩出错,以文件方式上传的,采用原文件上传
|
|
196
|
+ console.warn('Compress fail, dataURL is ' + dataURL + '. Next will use origin file to upload.');
|
|
197
|
+ callback(file);
|
|
198
|
+ }else{
|
|
199
|
+ let blob = dataURItoBlob(dataURL);
|
|
200
|
+ blob.id = file.id;
|
|
201
|
+ blob.name = file.name;
|
|
202
|
+ blob.lastModified = file.lastModified;
|
|
203
|
+ blob.lastModifiedDate = file.lastModifiedDate;
|
|
204
|
+ callback(blob);
|
|
205
|
+ }
|
|
206
|
+ }else{
|
|
207
|
+ if(/;base64,null/.test(dataURL) || /;base64,$/.test(dataURL)){
|
|
208
|
+ // 压缩失败,以base64上传的,直接报错不上传
|
|
209
|
+ options.onError(file, new Error('Compress fail, dataURL is ' + dataURL + '.'));
|
|
210
|
+ callback();
|
|
211
|
+ }else{
|
|
212
|
+ file.base64 = dataURL;
|
|
213
|
+ callback(file);
|
|
214
|
+ }
|
|
215
|
+ }
|
|
216
|
+ };
|
|
217
|
+ img.src = evt.target.result;
|
|
218
|
+ };
|
|
219
|
+ reader.readAsDataURL(file);
|
|
220
|
+}
|
|
221
|
+
|
|
222
|
+export default compress;
|