# Thread: Constrain images proportions.

1. ## Constrain images proportions.

Say we have an image (760px*1100px), but we want to resize that image into a 300px*300px box. Just making the image 300px*300px would distort the image, so we have to 'constrain' it's proportions, but I am unsure on how to do this.

My logic goes like this:

• Find images largest dimension (this case: height = 1100px)
• Find what percent 300 is of 1100px ((300 / 1100) * 100?)
• ... here is where my logic ends.

Any ideas? Is my math for working the percentage out correct? .. and would I then just apply this percentage to working out the smaller dimension?

2. 1100/100 = 11, which is 1%

so 300/11 = 27%

If you resized the image so that both lengths were 27% of the original then it wouldn't look distorted, but it also wouldn't be 300x300, as 300 is only 27% of 1100. You can't have no distortion and 300x300, just one.

3. Originally Posted by Bruce
1100/100 = 11, which is 1%

so 300/11 = 27%

If you resized the image so that both lengths were 27% of the original then it wouldn't look distorted, but it also wouldn't be 300x300, as 300 is only 27% of 1100. You can't have no distortion and 300x300, just one.
Thanks. I wasn't saying I wanted it 300*300, but an image scaled to fit in there elegantly.

Anyway, I've gotten it done.. I think. Is this correct:

[PHP]

// 1000 pixels wide
$imageX = 1000; // 2000 pixels tall$imageY = 2000;

// find the largest dimension
$largest = ($imageY > $imageX ) ?$imageY : $imageX; // your proportion$constrainTo = 300;

$percent = floor(($constrainTo / $largest) * 100);$newImageY = ($imageY / 100) *$percent;
$newImageX = ($imageX / 100) * \$percent;

[/PHP]