У меня есть функция, которую я использую, чтобы определить абсолютный максимальный размер изображения (при сохранении его соотношения сторон) в пределах определенного прямоугольника. Код, который я сейчас использую, определенно неверен, но я не уверен, где я ошибся.
// Sets the image to the largest proportional size possible
// based on the current browser window dimensions and original
// image size specified by the image loader
function applyLargestProportionalSize() {
var maxWidth = <width of container>;
var maxHeight = <height of container>;
var realWidth = <actual image width>;
var realHeight = <actual image height>;
if (realWidth < realHeight && maxWidth > maxHeight) {
var scaledWidth = Math.min(realWidth, maxWidth);
$('img').css('width', scaledWidth);
// let height be determined by the browser
} else {
var scaledHeight = Math.min(realHeight, maxHeight);
$('img').css('height', scaledHeight);
// let width be determined by the browser
}
}