Blame view

public/assets/addons/fastflow/plugins/compression/compression.js 3.32 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
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
  define([], function () {

      var Compression = {

          /**

           * 以obj.height 为基准 压缩

           * baseImage base64 图片

           * obj { width: 900, height: 35 }

           * quality 图像质量  0 - 1   1 质量越高

           */

          compress: function (baseImage, obj, quality, callback) {

              var img = new Image();

              img.src = baseImage;

              img.onload = function () {

                  var that = this;

                  var w = that.width, h = that.height;

                  //按照h 压缩后的base64 和 width

                  var compressionResult, compressionWidth, compressionHeight;

                  if (h > obj.height) {

                      //生成canvas

                      var canvas = document.createElement('canvas');

                      var ctx = canvas.getContext('2d');

                      //创建属性节点

                      var createw = document.createAttribute('width');

                      var createh = document.createAttribute('height');

                      h = obj.height;

                      w = (h / that.height) * w;

                      compressionWidth = w;

                      compressionHeight = h;

                      createw.nodeValue = w;

                      createh.nodeValue = h;

                      canvas.setAttributeNode(createw);

                      canvas.setAttributeNode(createh);

                      ctx.drawImage(that, 0, 0, w, h);

                      var base64 = canvas.toDataURL('image/png', quality);

                      compressionResult = base64;

                  } else {

                      compressionResult = baseImage;

                      compressionWidth = that.width;

                      compressionHeight = that.height;

                  }

                  //width > 默认width  做截取处理

                  if (compressionWidth > obj.width) {

                      Compression.Clipping(compressionResult, obj.width, compressionHeight, quality, function (base64Clipping) {

                          callback(base64Clipping);

                      });

                  } else {

                      callback(compressionResult);

                  }

              };

          },

          /**

           * 图片截取

           * base64Codes  图片base64 码

           * 从 (0,0) 开始截取   宽高分别为 width,height

           * quality  图片质量  0 - 1 ,  1 图片质量最高

           * callback  返回的是 截取后的base64 字符串

           */

          Clipping: function (base64Codes, width, height, quality, callback) {

              var img = new Image();

              img.src = base64Codes;

              //生成canvas

              var canvas = document.createElement('canvas');

              var ctx = canvas.getContext('2d');

              var createw = document.createAttribute('width');

              var createh = document.createAttribute('height');

              createw.nodeValue = width;

              createh.nodeValue = height;

              canvas.setAttributeNode(createh);

              canvas.setAttributeNode(createw);

              img.onload = function () {

                  ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);

                  var base64Result = canvas.toDataURL('image/png', quality);

                  callback(base64Result);

              };

          }

      };

      return Compression;

  });