@@ -33,7 +33,8 @@ Uploader = (function(superClass) {
33
33
headers : null ,
34
34
params : null ,
35
35
fileKey : 'upload_file' ,
36
- connectionCount : 3
36
+ connectionCount : 3 ,
37
+ compress : true
37
38
} ;
38
39
39
40
Uploader . prototype . _init = function ( ) {
@@ -70,7 +71,8 @@ Uploader = (function(superClass) {
70
71
} ) ( ) ;
71
72
72
73
Uploader . prototype . upload = function ( file , opts ) {
73
- var f , i , key , len ;
74
+ var f , i , key , len , _this ;
75
+ _this = this ;
74
76
if ( opts == null ) {
75
77
opts = { } ;
76
78
}
@@ -102,8 +104,12 @@ Uploader = (function(superClass) {
102
104
if ( this . triggerHandler ( 'beforeupload' , [ file ] ) === false ) {
103
105
return ;
104
106
}
105
- this . files . push ( file ) ;
106
- this . _xhrUpload ( file ) ;
107
+
108
+ this . compressImageFile ( file . obj , function ( blob_data ) {
109
+ file . obj = blob_data ;
110
+ _this . files . push ( file ) ;
111
+ _this . _xhrUpload ( file ) ;
112
+ } ) ;
107
113
return this . uploading = true ;
108
114
} ;
109
115
@@ -123,7 +129,8 @@ Uploader = (function(superClass) {
123
129
name : name ,
124
130
size : ( ref1 = fileObj . fileSize ) != null ? ref1 : fileObj . size ,
125
131
ext : name ? name . split ( '.' ) . pop ( ) . toLowerCase ( ) : '' ,
126
- obj : fileObj
132
+ obj : fileObj ,
133
+ compress : this . opts . compress
127
134
} ;
128
135
} ;
129
136
@@ -234,6 +241,90 @@ Uploader = (function(superClass) {
234
241
}
235
242
} ;
236
243
244
+ Uploader . prototype . compressImageFile = function ( fileObj , callback ) {
245
+ var fileReader , img ;
246
+ if ( ! $ . isFunction ( callback ) ) {
247
+ return ;
248
+ }
249
+
250
+ if ( ! this . opts . compress ) {
251
+ return callback ( fileObj ) ;
252
+ }
253
+
254
+ img = new Image ( ) ;
255
+
256
+ if ( window . FileReader && FileReader . prototype . readAsDataURL && / ^ i m a g e / . test ( fileObj . type ) ) {
257
+ fileReader = new FileReader ( ) ;
258
+ var _this = this ;
259
+ fileReader . onload = function ( e ) {
260
+ img . src = e . target . result ;
261
+
262
+ //resize the image using canvas
263
+ var canvas = document . createElement ( "canvas" ) ;
264
+ var ctx = canvas . getContext ( "2d" ) ;
265
+ ctx . drawImage ( img , 0 , 0 ) ;
266
+ var MAX_WIDTH = 800 ;
267
+ var MAX_HEIGHT = 800 ;
268
+ var width = img . width ;
269
+ var height = img . height ;
270
+
271
+ // less than the limited size of the returned directly
272
+ if ( width < MAX_WIDTH && height < MAX_HEIGHT ) {
273
+ return callback ( fileObj ) ;
274
+ }
275
+
276
+ if ( width > height ) {
277
+ if ( width > MAX_WIDTH ) {
278
+ height *= MAX_WIDTH / width ;
279
+ width = MAX_WIDTH ;
280
+ }
281
+ } else {
282
+ if ( height > MAX_HEIGHT ) {
283
+ width *= MAX_HEIGHT / height ;
284
+ height = MAX_HEIGHT ;
285
+ }
286
+ }
287
+ canvas . width = width ;
288
+ canvas . height = height ;
289
+ var ctx = canvas . getContext ( "2d" ) ;
290
+ ctx . drawImage ( img , 0 , 0 , width , height ) ;
291
+
292
+ //change the dataUrl to blob data for uploading to server
293
+ var dataURL = canvas . toDataURL ( 'image/jpeg' ) ;
294
+ var blob = _this . dataURItoBlob ( dataURL ) ;
295
+
296
+ return callback ( blob ) ;
297
+
298
+ } ;
299
+ return fileReader . readAsDataURL ( fileObj ) ;
300
+ } else {
301
+ return callback ( fileObj ) ;
302
+ }
303
+ } ;
304
+
305
+ Uploader . prototype . dataURItoBlob = function ( dataURI ) {
306
+ // convert base64/URLEncoded data component to raw binary data held in a string
307
+ var byteString ;
308
+ if ( dataURI . split ( ',' ) [ 0 ] . indexOf ( 'base64' ) >= 0 )
309
+ byteString = atob ( dataURI . split ( ',' ) [ 1 ] ) ;
310
+ else
311
+ byteString = unescape ( dataURI . split ( ',' ) [ 1 ] ) ;
312
+
313
+ // separate out the mime component
314
+ var mimeString = dataURI . split ( ',' ) [ 0 ] . split ( ':' ) [ 1 ] . split ( ';' ) [ 0 ] ;
315
+
316
+ // write the bytes of the string to a typed array
317
+ var ia = new Uint8Array ( byteString . length ) ;
318
+ for ( var i = 0 ; i < byteString . length ; i ++ ) {
319
+ ia [ i ] = byteString . charCodeAt ( i ) ;
320
+ }
321
+
322
+ return new Blob ( [ ia ] , {
323
+ type : mimeString
324
+ } ) ;
325
+
326
+ } ;
327
+
237
328
Uploader . prototype . destroy = function ( ) {
238
329
var file , i , len , ref ;
239
330
this . queue . length = 0 ;
0 commit comments