Jump to content

Simple Height And Width Calculation


dweb

Recommended Posts

hi

 

i'm having big problems trying to figure something out, so wondering if someone can help.

 

I have two numbers, as

 

$width = 800;
$height = 1000;

 

these numbers are not always the same value, so they can look like

 

$width = 700;
$height = 450;

 

and i want to reduce both numbers, keeping their ratio

 

but i want to reduce both the values, until either one meets their min value allowed

 

$width_max_val = 200;
$height_min_val = 400;

 

So if the values were

 

$width = 410;
$height = 750;

$width_max_val = 200;
$height_min_val = 400;

 

then the outputted values would be

 

219

400

 

can anyone help?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/271016-simple-height-and-width-calculation/
Share on other sites

Um, your question doesn't match your "code". You state " . . . i want to reduce both the values, until either one meets their min value allowed"

 

But, in the variables you have $width_max_val and $height_min_val. Huh???

 

Then in your last example, you show that you want the results to be 219 and 400, but 219 is greater than 200 which is supposedly the min, or is it max? It just doesn't make sense. If you have a minimum for one measurement but a max for the other you could very easily have a scenario where there is no solution. But, if you really DO want to set the new measurements based upon two minimums, it is simple math.

 //Determine the smaller of the input to min ratio
$resizeRatio = min( ($minWidth/$inputWidth), ($minHeight/$inputHeight) );

//Get new sizes based on resize ratio
$newWidth = round($inputWidth * $resizeRatio);
$newHeight = round($inputHeight * $resizeRatio);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.