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
 
var Orientation = null;
var upUrl = '';
function previewImage(obj,type,upurl=''){
    upUrl = upurl;
 
    setFilesReader(obj.files[0], 0,type);
}
function setFilesReader(file, ismul,type) {
    //获取照片方向角属性,用户旋转控制  
    EXIF.getData(file, function() {     
        Orientation = EXIF.getTag(this, 'Orientation');  
    });  
        
    var reader = new window.FileReader();
    reader.onload = function(e) {
        compress(this.result, fileSize, ismul,type);
    }
    reader.readAsDataURL(file);
    //console.log(this.files[0]);
    var fileSize = Math.round(file.size/1024/1024) 
}
 
//onchange="readMultiFiles(this.files)"
function readMultiFiles(files) {
    for (var i = 0; i < files.length; i++) {
        setFilesReader(files[i]);
    }
}
 
//res代表上传的图片,fileSize大小图片的大小
function compress(res, fileSize, ismul,type) {
    var img = new Image(), maxW = 1000; //设置最大宽度
 
    img.onload = function () {
        var cvs = document.createElement('canvas'), ctx = cvs.getContext('2d');
 
        if(img.width > maxW) {
            img.height *= maxW / img.width;
            img.width = maxW;
        }
 
        var w = img.width,
            h = img.height;
            
        cvs.width = w;
        cvs.height = h;
 
        ctx.clearRect(0, 0, cvs.width, cvs.height);
 
        if (Orientation != "" && Orientation != 1 && Orientation != undefined) {
 
            switch (Orientation) {
                case 6://需要顺时针90度旋转
                    cvs.width = h;
                    cvs.height = w;
                    ctx.rotate(Math.PI / 2);
                    ctx.drawImage(img, 0, -h, w, h);
                    break;
                case 8://需要逆时针90度旋转
                    cvs.width = h;
                    cvs.height = w;
                    ctx.rotate(-90 * Math.PI / 180);
                    ctx.drawImage(img, -w, 0, w, h);
                    break;
                case 3://需要180度旋转
                    ctx.rotate(180 * Math.PI / 180);
                    ctx.drawImage(img, -w, -h, w, h);
                    break;
            }
        }else{
            ctx.drawImage(img, 0, 0, img.width, img.height);
        }
        
        var compressRate = getCompressRate(1,fileSize);
 
        var dataUrl = cvs.toDataURL('image/jpeg', compressRate);
 
        if(upUrl!=''){
            showLoading();
            $.post(upUrl,{preview:dataUrl},function(res) {
                hideLoading();
                if(res){
                    if(res.error == 1){
                        $('#'+type).val(res.picurl);
                    }else{
                        showToast(res.msg);
                        return false;
                    }
                }
            }, 'json');
        }else{
            if($('#'+type)){
                $('#'+type).val(dataUrl);
            }
        }
 
        if($('#'+type+'img')){
            $('#'+type+'img').attr('src',dataUrl);
        }
        if($('#'+type+'show')){
            $('#'+type+'show').css('display', 'block');
        }
    }
    img.src = res;
}
 
//计算压缩比率,size单位为MB
function getCompressRate(allowMaxSize,fileSize){
    var compressRate = 1;
        
    if(fileSize/allowMaxSize > 4){
       compressRate = 0.5;
    } else if(fileSize/allowMaxSize >3){
       compressRate = 0.6;
    } else if(fileSize/allowMaxSize >2){
       compressRate = 0.7;
    } else if(fileSize > allowMaxSize){
       compressRate = 0.8;
    } else{
       compressRate = 0.9;
    }
 
    return compressRate;
}
function photoChange(obj){
    var files = obj.files,
        file;
    if(files && files.length) {
        showLoading();
        previewImage(obj,'preview');
        file = files[0];
        if(/^image\/\w+$/.test(file.type)) {
            setTimeout(function(){
                hideLoading();
                toAlloyCrop($('#preview').val());
            },1000);
            obj.value = '';
        } else {
            showToast('请上传图片');
        }
    }
}