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
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);

Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.