这个函数是为了实现图片按比例缩放的功能,确保图像在指定的尺寸范围内进行优化显示。
具体函数如下
export const getImgWidthHeight = (imgWidth, imgHeight) => {
const maxDimension = 150;
const minDimension = 30;
let width = Math.min(imgWidth, maxDimension);
let height = Math.min(imgHeight, maxDimension);
if (imgWidth > imgHeight && imgWidth > maxDimension) {
height = (maxDimension / imgWidth) * imgHeight;
} else if (imgHeight > imgWidth && imgHeight > maxDimension) {
width = (maxDimension / imgHeight) * imgWidth;
}
width = Math.max(width, minDimension);
height = Math.max(height, minDimension);
return {
width,
height
};
};