cellsysArt/artImage.js

50 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2024-12-26 10:28:43 +08:00
class ArtImage {
constructor(url) {
this.url = url; //原图路径
}
get compressionUrl() {
return formatCompression(this.url);
}
get previewUrl() {
return formatPreview(this.url);
}
}
function formatCompression(url) {
if (!url) return;
if (url.indexOf('blob:') !== -1) return url; //本地文件流直接放行
if (url.indexOf('/compression/') !== -1) {
url = url.replace('/compression/', '/');
}
if (url.indexOf('/preview/') !== -1) {
url = url.replace('/preview/', '/');
}
let imgSrc = url;
let splitList = imgSrc.split('/');
let fileName = splitList[splitList.length - 1];
fileName = fileName.split('.')[0] + '.jpg'; //图片服务压缩过的图片统一都是jpg格式因此这里需要更好图片后缀
splitList.pop(); //移除掉文件名部分
return splitList.join('/') + '/compression/' + fileName;
}
//转化预览图路径
function formatPreview(url) {
if (!url) return;
if (url.indexOf('blob:') !== -1) return url; //本地文件流直接放行
if (url.indexOf('/preview/') !== -1) return url;
if (url.indexOf('/compression/') !== -1) {
url = url.replace('/compression/', '/');
}
let imgSrc = url;
let splitList = imgSrc.split('/');
let fileName = splitList[splitList.length - 1];
fileName = fileName.split('.')[0] + '.jpg'; //图片服务压缩过的图片统一都是jpg格式因此这里需要更好图片后缀
splitList.pop(); //移除掉文件名部分
return splitList.join('/') + '/preview/' + fileName;
}
export default ArtImage;