문제

예를 들어, 폭 100px x 50px 인 경계 상자를 채워야한다면 다음 입력 이미지는 다음과 같은 동작을 갖습니다.

  1. 200W X 200H는 50%로 축소되고 25%는 상단과 하단에서 잘려옵니다.

  2. 200W X 100H는 자르기없이 50% 줄어 듭니다.

  3. 100W X 200H Gets는 스케일링되지 않았지만 75px는 상단과 하단에서 잘게 썰어집니다.

이것은 일반적인 크기 조정 기능 인 것처럼 보이지만 알고리즘의 예를 추적 할 수 없었습니다.

의사 코드를 포함한 모든 언어로 답변을받습니다. 답변이있는 페이지에 대한 링크도 좋습니다!

도움이 되었습니까?

해결책

당신이 요구하는 것은 매우 쉽습니다. 너비와 높이의 다양한 스케일링 계수를 계산 한 다음 실제 스케일 계수에 대해 더 큰 제품을 선택하십시오. 입력 크기에 스케일을 곱하고 너무 큰 것을 자르십시오.

scale = max(maxwidth/oldwidth, maxheight/oldheight)
scaledwidth = oldwidth * scale
scaledheight = oldheight * scale
if scaledheight > maxheight:
    croptop = (scaledheight - maxheight) / 2
    cropbottom = (scaledheight - maxheight) - croptop
if scaledwidth > maxwidth:
    cropleft = (scaledwidth - maxwidth) / 2
    cropright = (scaledwidth - maxwidth) - cropleft

다른 팁

여기서 우리는 x가 100%보다 큰 경우에만 확장해야합니다. 그런 다음 우리가 그렇게 한 후에, 우리는 Y에서 50 px 만 보장합니다. 50보다 큰 경우, 차이를 2로 나누고 상단/하단에서 양을 제거합니다.

double percent_x = 1.0;

if(X > 100) {
 percent_x = (float)100/X;
 X *= percent_x;
 Y *= percent_x;
}

int diff_y;
int top_cut, bott_cut;
if( Y > 50 ) {
 diff_y = (Y - 50) / 2;
 top_cut = bott_cut = diff_y;
}

크게 Mark Ransom의 답변에서 영감을 얻었습니다 (정말 감사합니다 - 저를 구했습니다). 이것을하고 싶은 사람이라면 누구나 없이 이미지 자르기 (한계 안에 맞음), 나는 이것이 효과가 있음을 발견했습니다.

if (maxWidth > width && maxHeight > height) {
  return { width, height };
}

aspectRatio = width / height,
scale       = max(maxWidth / width, maxHeight / height);

scaledHeight = height * scale,
scaledWidth  = width * scale;

if (scaledHeight > maxHeight) {
  scaledHeight = maxHeight;
  scaledWidth  = aspectRatio * scaledHeight;
} else if (scaledWidth > maxWidth) {
  scaledWidth  = maxWidth;
  scaledHeight = scaledWidth / aspectRatio;
}

return { scaledHeight, scaledWidth };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top