download url : https://github.com/exif-js/exif-js

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
/* ======================================== 图片压缩方法 ======================================== */
function  compressImage(maxWidth,maxHeight,srcOriginalImage,orientation,callback){
     // 创建 canvas
     var  canvasId = "canvas_" + new  Date().getTime()+ "" +parseInt(Math.random());
     $( "<canvas></canvas>" ).hide().attr( "id" ,canvasId).appendTo( "body" );
 
     var  c=$( "#" +canvasId)[0];
     var  ctx=c.getContext( "2d" );
 
     // 释放canvas
     function  releaseCanvas(){
         $( "#" +canvasId).remove();
     }
 
 
     // 创建要绘制的Image对象
     var  img =  new  Image();
     img.src = srcOriginalImage;
     // 等待img加载完毕
     img.onload =  function (){
         // 与backgournd-size:contain效果相同
         if (img.width/img.height<maxWidth/maxHeight){
             c.height = img.height;
             if (img.height>maxHeight){
                 c.height = maxHeight;
             }
             c.width = img.width/img.height*c.height;
         } else {
             c.width = img.width;
             if (img.width>maxWidth){
                 c.width = maxWidth;
             }
             c.height = img.height/img.width*c.width;
         }
 
 
 
         var  compressImageWidth = c.width;
         var  compressImageHeight = c.height;
 
         if (orientation==6){
             var  tmp = c.width;
             c.width = c.height;
             c.height= tmp;
 
             ctx.translate(c.width,0);
             ctx.rotate(Math.PI/180*90);
         }
         ctx.drawImage(img,0,0,img.width,img.height,0,0,compressImageWidth,compressImageHeight);
 
         callback(c.toDataURL());
 
         releaseCanvas();
     };
}