chengkun
2025-05-26 8f3df543230cd4403368b39b9bbe5726d11a0284
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
(function( factory ) {
    if ( !window.jQuery ) {
        alert('jQuery is required.')
    }
 
    jQuery(function() {
        factory.call( null, jQuery );
    });
})(function( $ ) {
// -----------------------------------------------------
// ------------ START ----------------------------------
// -----------------------------------------------------
 
// ---------------------------------
// ---------  Uploader -------------
// ---------------------------------
var Uploader = (function() {
 
    // -------setting-------
    // 如果使用原始大小,超大的图片可能会出现 Croper UI 卡顿,所以这里建议先缩小后再crop.
    var FRAME_WIDTH = 400; 
    var _ = WebUploader;
    var Uploader = _.Uploader;
    var uploaderContainer = $('.uploader-container');
    var uploader, file; 
    if ( !Uploader.support() ) {
        layer.alert( 'Web Uploader 不支持您的浏览器!'); 
    }
 
    // hook,
    // 在文件开始上传前进行裁剪。
    Uploader.register({
        'before-send-file': 'cropImage'
    }, {
 
        cropImage: function( file ) {
 
            var data = file._cropData,
                image, deferred;
 
            file = this.request( 'get-file', file );
            deferred = _.Deferred();
 
            image = new _.Lib.Image();
 
            deferred.always(function() {
                image.destroy();
                image = null;
            });
            image.once( 'error', deferred.reject );
            image.once( 'load', function() {
                image.crop( data.x, data.y, data.width, data.height, data.scale );
            });
 
            image.once( 'complete', function() {
                var blob, size;
 
                // 移动端 UC / qq 浏览器的无图模式下
                // ctx.getImageData 处理大图的时候会报 Exception
                // INDEX_SIZE_ERR: DOM Exception 1
                try {
                    blob = image.getAsBlob();
                    size = file.size;
                    file.source = blob;
                    file.size = blob.size;
 
                    file.trigger( 'resize', blob.size, size );
 
                    deferred.resolve();
                } catch ( e ) {
                    console.log( e );
                    // 出错了直接继续,让其上传原始图片
                    deferred.resolve();
                }
            });
 
            file._info && image.info( file._info );
            file._meta && image.meta( file._meta );
            image.loadFromBlob( file.source );
            return deferred.promise();
        }
    });
 
    return {
        init: function( selectCb ) {
            uploader = new Uploader({
                pick: {
                    id: '#filePicker',
                    multiple: false
                },
 
                // 设置用什么方式去生成缩略图。
                thumb: {
                    quality: 100,
 
                    // 不允许放大
                    allowMagnify: false,
 
                    // 是否采用裁剪模式。如果采用这样可以避免空白内容。
                    crop: false
                },
 
                // 禁掉分块传输,默认是开起的。
                chunked: false,
 
                // 禁掉上传前压缩功能,因为会手动裁剪。
                compress: false,
 
                // fileSingleSizeLimit: 2 * 1024 * 1024,
 
                server: serverPath,
                swf:  tplPath+'js/webuploader/Uploader.swf',
                fileNumLimit: 1,
                onError: function() {  
                    var args = [].slice.call(arguments, 0);
                    alert(args.join('\n'));
                } 
            });
            
            
            uploader.on( 'uploadProgress', function( file, percentage ) { 
                layer.msg('执行中……',{icon:16,time:100000});return;
            });
            uploader.on( 'uploadSuccess', function( file ,st) {
                if(st=='1'){
                    layer.msg('上传成功!',{icon:6},function(){location.reload();});
                }else{
                    layer.msg('上传失败!',{icon:5},function(){location.reload();});    
                } 
            }); 
            uploader.on('fileQueued', function( _file ) {
                file = _file;
 
                uploader.makeThumb( file, function( error, src ) {
 
                    if ( error ) {
                        layer.msg('不能预览!',{icon:5});return;
                    }
 
                    selectCb( src );
 
                }, FRAME_WIDTH, 1 );   // 注意这里的 height 值是 1,被当成了 100% 使用。
            });
        },
 
        crop: function( data ) {
 
            var scale = Croper.getImageSize().width / file._info.width;
            data.scale = scale;
 
            file._cropData = {
                x: data.x1,
                y: data.y1,
                width: data.width,
                height: data.height,
                scale: data.scale
            };
        },
 
        upload: function() {
            uploader.upload();
        }
    }
})();
 
// ---------------------------------
// ---------  Crpper ---------------
// ---------------------------------
var Croper = (function() {
    var container = $('.cropper-wraper');
    var $image = container.find('.img-container img');
    var btn = $('.upload-btn');
    var isBase64Supported, callback;
 
    $image.cropper({
        aspectRatio: imgwidth / imgheight,
        preview: ".img-preview",
        done: function(data) {
            //console.log(data);
        }
    });
 
    function srcWrap( src, cb ) {
 
        // we need to check this at the first time.
        if (typeof isBase64Supported === 'undefined') {
            (function() {
                var data = new Image();
                var support = true;
                data.onload = data.onerror = function() {
                    if( this.width != 1 || this.height != 1 ) {
                        support = false;
                    }
                }
                data.src = src;
                isBase64Supported = support;
            })();
        }
 
        if ( isBase64Supported ) {
            cb( src );
        } else {
            // otherwise we need server support.
            // convert base64 to a file.
            $.ajax(previewPath, {
                method: 'POST',
                data: src,
                dataType:'json'
            }).done(function( response ) {
                if (response.result) { 
                    cb( response.result );
                } else {
                    layer.msg('预览出错!',{icon:5});return false; 
                }
            });
        }
    }
 
    btn.on('click', function() {
        callback && callback($image.cropper("getData"));
        return false;
    });
 
    return {
        setSource: function( src ) {
 
            // 处理 base64 不支持的情况。
            // 一般出现在 ie6-ie8
            srcWrap( src, function( src ) {
                $image.cropper("setImgSrc", src);
            });
 
            container.removeClass('webuploader-element-invisible');
 
            return this;
        },
 
        getImageSize: function() {
            var img = $image.get(0);
            return {
                width: img.naturalWidth,
                height: img.naturalHeight
            }
        },
 
        setCallback: function( cb ) {
            callback = cb;
            return this;
        },
 
        disable: function() {
            $image.cropper("disable");
            return this;
        },
 
        enable: function() {
            $image.cropper("enable");
            return this;
        }
    }
 
})();
 
 
// ------------------------------
// -----------logic--------------
// ------------------------------
var container = $('.uploader-container');
 
Uploader.init(function( src ) {
 
    Croper.setSource( src );
 
    // 隐藏选择按钮。
    container.addClass('webuploader-element-invisible');
 
    // 当用户选择上传的时候,开始上传。
    Croper.setCallback(function( data ) {
        Uploader.crop(data);
        Uploader.upload();
    });
});
 
 
 
// -----------------------------------------------------
// ------------ END ------------------------------------
// -----------------------------------------------------
});