image-handler.js
1.09 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
const MAX_WIDTH = 1000;
export function getArticleImageSize({width, height, MIN_SCALE = 0.75}) {
width = +width;
height = +height;
if (width > MAX_WIDTH) {
height = height / (width / MAX_WIDTH);
width = MAX_WIDTH;
}
if (MIN_SCALE && width / height < MIN_SCALE) {
height = width / MIN_SCALE;
}
if (width === 1) {
width = MAX_WIDTH;
}
if (height === 1) {
height = MAX_WIDTH;
}
return {width, height};
}
export function processImage(src, mode, width, height, webp) {
let splits = (src || '').split('?');
const imgName = splits[0] || '';
let query = splits[1] || '';
if (src.indexOf('{width}') < 0) {
return src;
}
if (/imageView/.test(query)) {
if (!/interlace/.test(query)) {
src = `${src}/interlace/1`;
}
if (!/webp/.test(query) && webp) {
src = `${src}/format/webp`;
} else if (!/format/.test(query) && /\.png$/i.test(imgName)) {
src = `${src}/format/jpg`;
}
}
return (src || '')
.replace('http://', 'https://')
.replace('{mode}', mode)
.replace('{width}', width)
.replace('{height}', height);
}