/
index.ts
303 lines (281 loc) · 6.86 KB
/
index.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const COMPRESS_PREFIX = /*#__PURE__*/ "?basic=";
/**
* 获取文件后缀
* @param path 文件名
*/
export function isGif(path: string): boolean {
return path && path.toLowerCase().substring(path.length - 4, path.length) === ".gif";
}
/**
* URL预处理
*/
function replaceUrl(url: string, domainList: Array<string | RegExp>, cdnSite: string): string {
if (typeof url !== "string") {
(console.error || console.log)("[cdn]invalid url:", url);
} else {
url = url.split(COMPRESS_PREFIX)[0];
domainList.forEach(e => (url = url.replace(e, cdnSite)));
}
return url;
}
/**
* 配置信息
*/
export const config = /*#__PURE__*/ {
/**
* 宽度, 默认2K屏
*/
Width: 1440,
/**
* 屏幕高度, 默认2K屏幕
*/
Height: 2560,
/**
* 图片质量1~100
*/
Quality: 100,
/**
* 缩略图宽度
*/
ThumbnailWidth: 138,
/**
* 缩略图宽度1~100
*/
ThumbnailQuality: 1,
/**
* 代替换的域名列表
*/
DomainList: [] as Array<string | RegExp>,
/**
* 图片处理CDN
*/
ImageCDN: "",
/**
* 媒体流加速CDN
*/
StreamCDN: "",
isAndroid: false,
};
/**
* 生成压缩URL
* 非http/https资源不处理
* @param option 压缩参数
* @param cdnSite CDN域名 如 `https://mycdn.net/` 空则不替换
* @param domainList 域名列表 `[/^https?:\/\/[\w\-\.]*\//]`
*/
export function compress(option: CDNOptions, cdnSite?: string, domainList: Array<string | RegExp> = []): string {
if (option.url.indexOf('http') !== 0) {
return option.url;
}
const url = cdnSite ? replaceUrl(option.url, domainList, cdnSite) : option.url;
let param: string = "";
if (option.width) {
param += `_${option.width}w`;
}
if (option.height) {
param += `_${option.height}h`;
}
if (option.quality && option.quality < 100) {
param += `_${option.quality}q`;
}
if (option.progressive) {
param += `_1pr`;
}
if (option.scaleType) {
param += `_${option.scaleType}e`;
}
if (option.cut) {
param += `_1c`;
}
if (option.ext) {
param += option.ext;
}
return param ? url + COMPRESS_PREFIX + (option.larger ? "0l" : "1l") + param + (option.acceptraw ? '&p=acceptraw' : '') : url;
}
/**
* 屏幕宽度自适应图像
* @param url url of image
*/
export function adaptiveImage(url: string): string {
return isGif(url)
? url // gif 不处理
: compress(
{
acceptraw: true,
url: url,
width: config.Width,
quality: config.Quality,
progressive: config.Width > 800,
ext: config.isAndroid ? ".webp" : ".jpg",
},
config.ImageCDN,
config.DomainList,
);
}
/**
* 横屏自适应图像
* @param url url of image
*/
export function adaptiveLandscapeImage(url: string): string {
const width = config.Width > config.Height ? config.Width : config.Height;
return isGif(url)
? url // gif 不处理
: compress(
{
acceptraw: true,
url: url,
width: width,
quality: config.Quality,
progressive: width > 800,
ext: config.isAndroid ? ".webp" : ".jpg",
},
config.ImageCDN,
config.DomainList,
);
}
/**
* 渐进式加载大图
* @param url image URL
*/
export function progressiveImage(url: string): string {
return isGif(url)
? url // gif 不处理
: compress(
{
acceptraw: true,
url: url,
progressive: true,
ext: config.isAndroid ? ".webp" : ".jpg",
},
config.ImageCDN,
config.DomainList,
);
}
/**
* 生成好友对话消息的缩略图
* @param url image Url
*/
export function shareImage(url: string): string {
return isGif(url)
? url
: compress(
{
acceptraw: true,
url: url,
width: 500,
height: 400,
cut: true,
scaleType: 1,
quality: 75,
larger: true,
ext: ".jpg",
},
config.ImageCDN,
config.DomainList,
);
}
/**
* 小尺寸缩略图
* @param url 原url
*/
export function thumbnail(url: string): string {
return compress(
{
acceptraw: true,
url: url,
width: config.ThumbnailWidth,
quality: config.ThumbnailQuality,
ext: ".jpg",
},
config.ImageCDN,
config.DomainList,
);
}
/**
* 流媒体
* @param url 原url
*/
export function streamCdn(url: string): string {
return config.StreamCDN ? replaceUrl(url, config.DomainList, config.StreamCDN) : url;
}
/**
* 压缩头像
* @param url
* @param size
*/
export function compressAvatar(url: string, size: 0 | 46 | 64 | 96 | 132): string {
if (url.indexOf("https://wx.qlogo.cn") === 0) {
return url.substring(0, url.lastIndexOf("/") + 1) + size;
}
return isGif(url)
? url
: compress(
size > 0
? {
url: url,
width: size,
height: size,
cut: true,
scaleType: 1,
ext: ".jpg",
acceptraw: true,
}
: {
url: url,
ext: ".jpg",
acceptraw: true,
},
config.ImageCDN,
config.DomainList,
);
}
/**
* CDN 压缩参数
*/
export interface CDNOptions {
/**
* 原始URL
*/
url: string;
/**
* 0 或 不定义 锁定宽高比且长边优先
1 锁定宽高比且短边优先
2 强制宽高
4 锁定宽高比且短边优先,并在缩放后以指定颜色填充空白区域(若颜色未指定则自动采用白色填充
*/
scaleType?: 0 | 1 | 2 | 4;
/**
* 值为 0 或 1 的整数,默认为 0 即不进行其他操作,为1则自动裁剪原图至指定尺寸
*/
cut?: boolean;
/**
* 输出宽度
*/
width?: number;
/**
* 输出高度
*/
height?: number;
/**
* 图片质量默认100
*/
quality?: number;
/**
* 是否渐进加载
*/
progressive?: boolean;
/**
* 图片是否放大
* 默认值为false,
* 为 `true` 即允许将图片缩放至大于原图的尺寸,
*/
larger?: boolean;
/**
* 输出格式
*/
ext?: ".jpg" | ".webp" | ".png" | ".jpeg" | ".gif";
/**
* 处理失败是否返回原图
*/
acceptraw?: boolean;
}