제대로 사용해야 가로 세로 비율을 유지하려면 GCD를 :
function calcRatio(objectWidth, objectHeight) {
var ratio = gcd(objectWidth, objectHeight),
xRatio = objectWidth/ratio,
yRatio = objectHeight/ratio;
return { "x": xRatio, "y": yRatio };
}
:
function gcd(a, b) {
return (b == 0) ? a : gcd(b, a % b);
}
당신은 다음과 같은 뭔가해야
이것은 어떤 개체의 비율을 얻을 것입니다. 이 정보를 사용하여 결과 이미지의 크기를 파악할 수 있습니다.
function findSize (targetHeight, xRatio, yRatio) {
var widthCalc = Math.round((xRatio * targetHeight)/yRatio);
return { "width" : widthCalc, "height" : targetHeight };
}
var test = calcRatio(1280,720),
dimensions = findSize(400, test.x, test.y);
//400 is what we want the new height of the image to be
이들은 귀하의 크기입니다. 당신이 당신의 일을 할 필요가있는 당신의 이미지 주위에 여분의 "공간"이 없다면. 그렇지 않으면 두 경우를 처리해야합니다.