Resize img with min/max + ratio

Hi all

IANAM (I am not a mathematician) and this is causing me some problems.

I need a function that scales down its input variables based on the min/max range

function imageResize(&$width,&$height,$maxwidth,$maxheight)


I want that both width and height are in range AND

originalsize/originalwidth ~= newwidth/newheight

I tried first calculating width, and then decreasing that until width*ratio<=maxheight but that gives result way too low!

Good luck

Ramses aka x_terminat_or_3
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hi x_terminat_or_3,

Try the following:

function imageResize(&$width,&$height,$maxwidth,$maxheight) {
 if ($width < $height) {
  $width = floor(($maxheight / $height) * $width);
  $height = $maxheight;
 } else {
  $height = floor(($maxwidth / $width) * $height);
  $width = $maxwidth;

$width = 1234;
$height = 567;


echo "Width: $width Height: $height";

You might also be interested in reading through this thread: http:Q_21476397.html
Matt and i covered this topic with image uploads and resizing.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
x_terminat_or_3Author Commented:
Thanks my man.  Ugly simple that.  Shame I didn't see it myself.

Anyway, thanks for the link as well, but I cannot rely on GD being available so I 'cheat' by just setting the width/height attributes on the image tag.

With kind regards

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.