yiming
2024-03-13 f9d4b09377c5471e1202be2fef2c89de27b6654d
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
var isString = function (value) {
  return typeof value === 'string';
};
 
var isNumber = function (value) {
  return typeof value === 'number';
};
 
var getFileExt = function (src) {
  var fileUrl = src.split('?')[0];
  var splitUlr = fileUrl.split('/');
  var filepath = splitUlr[splitUlr.length - 1];
  return filepath.split('.')[1] || 'jpg';
};
 
function isUrl(url) {
  // NOCC:ToolNameCheck(非敏感词)
  var urlReg = getRegExp(
    '/[(http(s)?)://(www.)?a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/',
    'ig',
  );
 
  return urlReg.test(url);
}
 
function rpx2px(rpx, screenWidth) {
  // px / systemWidth = rpx / 750
  var result = (rpx * (screenWidth || 375)) / 750;
 
  return Math.round(result);
}
 
function imageMogr(url, options) {
  if (!isString(url) || !url) return '';
 
  if (
    url.indexOf('qlogo.cn') !== -1 ||
    url.indexOf('wxfile://') === 0 ||
    url.indexOf('http://tmp/wx') === 0 ||
    url.indexOf('imageMogr2') !== -1
  ) {
    //qlogo.cn域名或者本地图片不做转换
    return url;
  } //强制转https
 
  if (url.indexOf('http://') === 0) {
    url = url.replace('http://', 'https://');
  } else if (url.indexOf('//') === 0) {
    url = 'https:' + url;
  }
 
  if (!options) return url;
 
  var width = Math.ceil(options.width),
    height = Math.ceil(options.height),
    format = options.format,
    _optionsQuality = options.quality,
    quality = _optionsQuality === undefined ? 70 : _optionsQuality,
    _optionsStrip = options.strip,
    strip = _optionsStrip === undefined ? true : _optionsStrip,
    crop = options.crop;
  var isValidWidth = isNumber(width) && width > 0;
  var isValidHeight = isNumber(height) && height > 0;
  var imageMogrStr = '';
  var size = '';
 
  if (isValidWidth && isValidHeight) {
    size = ''.concat(width, 'x').concat(height);
  } else if (isValidWidth) {
    size = ''.concat(width, 'x');
  } else if (isValidHeight) {
    size = 'x'.concat(height);
  }
 
  if (size) {
    //缩放或者裁剪
    imageMogrStr += '/'.concat(crop ? 'crop' : 'thumbnail', '/').concat(size);
 
    if (crop) {
      //裁剪目前需求只有以图片中心为基准
      imageMogrStr += '/gravity/center';
    }
  }
 
  if (isNumber(quality)) {
    //质量变换
    imageMogrStr += '/quality/'.concat(quality);
  }
 
  if (strip) {
    //去除元信息
    imageMogrStr += '/strip';
  }
 
  var ext = getFileExt(url);
 
  // gif 图片不做格式转换,否则会损坏动图
  if (ext === 'gif') {
    imageMogrStr += '/cgif/1';
  } else if (format) {
    //格式转换
    imageMogrStr += '/format/'.concat(format);
  }
 
  if (format === 'jpg' || (!format && (ext === 'jpg' || ext === 'jpeg'))) {
    //渐进式 jpg 加载
    imageMogrStr += '/interlace/1';
  }
  if (!imageMogrStr) return url;
  return ''
    .concat(url)
    .concat(url.indexOf('?') !== -1 ? '&' : '?', 'imageMogr2')
    .concat(imageMogrStr);
}
function getSrc(options) {
  if (!options.src) return '';
 
  if (options.thumbWidth || options.thumbHeight) {
    return imageMogr(options.src, {
      width:
        options.mode !== 'heightFix'
          ? rpx2px(options.thumbWidth, options.systemInfo.screenWidth) *
            options.systemInfo.pixelRatio
          : null,
      height:
        options.mode !== 'widthFix'
          ? rpx2px(options.thumbHeight, options.systemInfo.screenWidth) *
            options.systemInfo.pixelRatio
          : null,
      format: options.webp ? 'webp' : null,
    });
  }
 
  return '';
}
 
module.exports = {
  imageMogr: imageMogr,
  getSrc: getSrc,
};