math-challenged needs formula to constrain images for php + gd image manipulation
August 13, 2007 3:15 PM   Subscribe

I need a formula or actual php code to generate a new height and width that will constrain the image between a maximum height and a maximum width while retaining the correct proportions of the original image.

I'm working on some php code to resize images via gd that are uploaded. I've got the upload and saving code going, and I have successfully coded the parts where I'm given a width OR a height to resize the image.

My problem is that, for another application, I need to set a max width AND a max height and resize the image within those limits while keeping the proportions of the orginal image.

Example:
max_height = 80
max_width = 140

If an uploaded image is 200x200, the resized dimensions would be 80x80 (constrained by the max height of 80). But, my problem is being able to come up with the formula/code that will do that and also for harder ones such as where the uploaded image is something like 500w by 200h. I need a formula or php code by which I end up with dimensions for the uploaded image where the width is no wider than 140 and the height no higher than 80 but where the dimensions are proportionate to 500:200.

Thanks!

Steven
posted by sjarvis to Computers & Internet (10 answers total) 3 users marked this as a favorite
 
i wrote this script a few years ago, so it's quite possibly crap, but it was fairly functional. I do not doubt there is a more elegant way to write it -- I just don't have a moment to review the code right now. Perhaps it will give you some ideas about your own code. This is the most relevant section:
//if not forcing, we'll prefer having width correct first.
$new_width=$max_width;
$new_height= floor(($max_width/$width)*$height);
if ($new_height>$max_height) {
$new_height=$max_height;
$new_width=floor(($max_height/$height)*$width);
}

posted by fishfucker at 3:37 PM on August 13, 2007


image ratio constraint=w/h=140/80=1.75

Calculate ratio of your image to be resized (w/h)

Lower than 1.75: set h to 80. new width=80*ratio.

higher than 1.75: set w to 140. new height=140/ratio
posted by vacapinta at 3:38 PM on August 13, 2007 [1 favorite]


icant.co.uk has a simple, yet exhaustive, tutorial that I've used in the past.
posted by niles at 3:44 PM on August 13, 2007


If you're using any implementation of ImageMagick, the "scale" directive let's you set a bounding box while preserving aspect ratio.
posted by rhizome at 4:11 PM on August 13, 2007


2nd'ing imagemagick. It does it all automatically for you.
posted by mphuie at 4:14 PM on August 13, 2007


If you care about presentation only, then the max-width and max-height CSS attributes may be of interest.
posted by cmiller at 4:14 PM on August 13, 2007


Best answer: The min function is useful for this sort of thing:

s=min(max_width/width, max_height/height)
new_width=s*width
new_height=s*height
posted by dsword at 4:51 PM on August 13, 2007


Response by poster: I tried out several of these solutions, and dsword's min-based solution turned out to be the smallest (in lines of code) and worked just fine.

Thanks to everyone for the help!

Steven
posted by sjarvis at 6:26 PM on August 13, 2007


Just out of curiousity, does anyone know which method would compute faster?
posted by tracert at 6:43 PM on August 13, 2007


Response by poster: I didn't try the ImageMagick route (even though it has a built-in constrain function) because I had already done the rest of that script using gd.

I tried all (I think) of rest, and they all seemed fairly speedy. The size of the uploaded image was a far more relevant factor than the speed of the various solutions here.

Steven
posted by sjarvis at 8:35 PM on August 13, 2007


« Older Does fish oil last like any other oil?   |   To sleep, perchance to dream... Newer »
This thread is closed to new comments.