img-src-handle.js
1.77 KB
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
require('../utils/webp-support');
var defaultQuality = 60;
/**
* 图片链接处理
*/
module.exports = function (imgUrl, params) {
if (!imgUrl) {
return imgUrl;
}
params = Object.assign({
q: defaultQuality
}, params);
var splits = imgUrl.split('?');
var url = splits[0];
var query = splits[1] || '';
if (!query || query === 'imageslim') {
if (window.supportWebp) {
url += '?imageView2/0/interlace/1/format/webp/q/'+params.q;
} else {
url += params.q === defaultQuality ? '?imageslim' : '?imageView2/0/interlace/1/q/'+params.q;
}
imgUrl = url;
} else if (/imageView/.test(query)) { // imageView2 || imageView
if (!/\/q\/\d+/.test(query)) {
imgUrl += '/q/'+params.q;
} else {
imgUrl = imgUrl.replace(/\/q\/\d+/g, '/q/' + params.q);
}
if (window.supportWebp && !/format\//i.test(query)) {
imgUrl += '/format/webp';
}
if (window.supportWebp && (/format\/png/i.test(query) || /format\/jpg/i.test(query))) {
imgUrl = imgUrl.replace(/format\/png/i, 'format/webp').replace(/format\/jpg/i, 'format/webp');
}
} else if (/imageMogr/.test(query)) {
if (!/\/quality\/\d+/.test(query)) {
imgUrl += '/quality/'+params.q;
} else {
imgUrl = imgUrl.replace(/\/quality\/\d+/g, '/quality/' + params.q);
}
if (window.supportWebp && !/format\//.test(query)) {
imgUrl += '/format/webp';
}
if (window.supportWebp && (/format\/png/i.test(query) || /format\/jpg/i.test(query))) {
imgUrl = imgUrl.replace(/format\/png/i, 'format/webp').replace(/format\/jpg/i, 'format/webp');
}
}
return imgUrl;
};