我理解接受的答案和它的作品,但我总觉得下面的方法是“最适合”的简单和简洁:
// prep
let maxWidth = 190,
maxHeight = 150;
let imgWidth = photo.width,
imgHeight = photo.height;
// calc
let widthRatio = maxWidth / imgWidth,
heightRatio = maxHeight / imgHeight;
let bestRatio = Math.min(widthRatio, heightRatio);
// output
let newWidth = imgWidth * bestRatio,
newHeight = imgHeight * bestRatio;