picsum-image
Version:
快速生成picsum图片,适用于UI未提供图片时设置占位图。
47 lines (46 loc) • 1.16 kB
JavaScript
const i = {
id: 0,
width: 200,
height: 200,
jpg: !1,
blur: 0,
grayscale: !1,
cache: !0
}, s = class s {
/**
* 生成图片URL
* @param { PicsumConfig } options 配置项
* @returns { string } 图片URL
*/
static generateUrl(e) {
let a = this.baseUrl;
const t = {
...i,
...e || {}
};
t.id && (a += `/id/${t.id}`), t.width && (a += `/${t.width}`), t.height && (a += `/${t.height}`), t.jpg && (a += ".jpg");
const r = {};
t.grayscale && (r.grayscale = t.grayscale), t.blur && (r.blur = t.blur), t.cache && (r.random = Math.floor(Math.random() * 1e4));
const l = Object.keys(r).map((h) => h + "=" + r[h]).join("&");
return l && (a += `?${l}`), a;
}
/**
* 随机生成一张图片
* @param { number } width 图片宽度
* @param { number } height 图片高度
* @param { boolean } cache 是否需要缓存,默认false
* @returns { string } 图片URL
*/
static randomImage(e, a, t = !1) {
return this.generateUrl({
width: e,
height: a,
cache: t
});
}
};
s.baseUrl = "https://picsum.photos";
let c = s;
export {
c as PicsumImage
};