image-handler.js
1.21 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
const MAX_WIDTH = 1000;
export function getArticleImageSize({width, height, minScale = 0.75, maxWidth = MAX_WIDTH}) {
width = +width;
height = +height;
if (width > maxWidth) {
height = height / (width / maxWidth);
width = maxWidth;
}
if (minScale && width / height < minScale) {
height = width / minScale;
}
if (width === 1) {
width = maxWidth;
}
if (height === 1) {
height = maxWidth;
}
return {width, height: Math.round(height)};
}
export function processImage(src, mode, width, height, webp) {
let splits = (src || '').split('?');
const imgName = splits[0] || '';
let query = splits[1] || '';
if (!query) {
src = `${src}?imageView2/{mode}/w/{width}/h/{height}`;
}
if (!src || 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);
}