perf: 优化缩略图

This commit is contained in:
kuaifan 2024-10-30 14:14:58 +08:00
parent b8852f821c
commit 1a7591314f
2 changed files with 26 additions and 16 deletions

View File

@ -529,7 +529,7 @@ import {MarkdownPreview} from "../store/markdown";
/**
* 图片尺寸比例超出
* @param params {{src: *, width: (number|*), height: (number|*), crops: {percentage: string, ratio: number}, scaleSize: (number)}}
* @param params {{src: *, width: (number|*), height: (number|*), crops: {ratio?: number, size?: string, percentage?: string, cover?: string, contain?: string}, scaleSize: (number)}}
* src, // 原图地址
* width, // 原图宽度
* height, // 原图高度
@ -537,24 +537,22 @@ import {MarkdownPreview} from "../store/markdown";
* scaleSize, // 返回尺寸缩放最高尺寸
*/
imageRatioHandle(params) {
if (!/\.(png|jpg|jpeg)$/.test(params.src)) {
return params
}
if (!$A.isJson(params.crops)) {
return params
}
params.src = $A.thumbRestore(params.src) + "/crop/" + Object.keys(params.crops).map(key => {
return `${key}:${params.crops[key]}`
}).join(",")
if ($A.imageRatioJudge(params.src)) {
params.src = $A.thumbRestore(params.src) + "/crop/" + Object.keys(params.crops).map(key => {
return `${key}:${params.crops[key]}`
}).join(",")
const ratio = $A.imageRatioExceed(params.width, params.height, params.crops.ratio)
if (ratio > 0) {
if (params.width > params.height) {
params.width = params.height * ratio;
} else {
params.height = params.width * ratio;
const ratio = $A.imageRatioExceed(params.width, params.height, params.crops.ratio)
if (ratio > 0) {
if (params.width > params.height) {
params.width = params.height * ratio;
} else {
params.height = params.width * ratio;
}
}
}
@ -567,6 +565,18 @@ import {MarkdownPreview} from "../store/markdown";
return params;
},
/**
* 判断图片地址是否满足比例缩放
* @param url
* @returns {boolean}
*/
imageRatioJudge(url) {
if (!/\.(png|jpg|jpeg)$/.test(url)) {
return false
}
return $A.getDomain(url) == $A.getDomain($A.mainUrl());
},
/**
* 图片尺寸比例超出
* @param width

View File

@ -52,7 +52,7 @@ export default {
imageStyle({width, height, thumb}, type = 'style') {
if (width && height) {
const ratioExceed = $A.imageRatioExceed(width, height, 3)
if (/\.(png|jpg|jpeg)$/.test(thumb) && ratioExceed > 0) {
if ($A.imageRatioJudge(thumb) && ratioExceed > 0) {
if (width > height) {
width = height * ratioExceed;
} else {
@ -91,7 +91,7 @@ export default {
imageSrc({width, height, thumb}) {
const ratioExceed = $A.imageRatioExceed(width, height, 3)
if (/\.(png|jpg|jpeg)$/.test(thumb) && ratioExceed > 0) {
if ($A.imageRatioJudge(thumb) && ratioExceed > 0) {
thumb = $A.thumbRestore(thumb) + `/crop/ratio:${ratioExceed},percentage:320x0`;
}
return thumb;