Aspect Ratio Logic
I'm having a hard time wrapping my head around the logic behind an aspect ratio of an image.
I have four variables, max_width, max_height, image_width, image_height. I want to reduce the image width and height until *both* fit into the max_width and max_height values.
Any ideas on what the logic would be for something like that? I've been playing with a while loop:
<?php
while($image_width > $max_width) || ($image_height > $max_height))
{
// reduce the image_width and height proportionally, but how?
$image_height -= $some_value;
$image_width -= $some_value;
}
?>
I guess I just need to know what I subtract from the image height and width to keep the aspect ratio. Maybe there's a better way to do this?
kazimmerman posted this at 03:05 — 7th June 2008.
He has: 698 posts
Joined: Jul 2005
Are the max width and max height variables defined globally (for all images) or individually? I guess the only problem I'm having is deciding is if there's a way you would know beforehand if the width or height was larger. If not, you could run a test to find out and do something different in each case. I'm working on a code right now, and I'll post it when I'm finished, but I wanted to see if that might set you off in the right direction.
Edit: Alright, here's what I got. I checked it on rectangular and square images alike, and the following complete code works. Obviously you can cut out some parts of it and rework the getimagesize() function to suit multiple images.
<?php
$imagesize = getimagesize(""); //Path to image
$image_width = $imagesize[0]; //Width of image
$image_height = $imagesize[1]; //Height of image
$max_width = ""; //These two need to be defined.
$max_height = "";
while(($image_width > $max_width) || ($image_height > $max_height)) {
if ($image_width > $image_height) {
/*Now find the proportion of width to height and use it to determine the new height. The new width will be the max width.*/
$prop = ($image_width / $image_height);
$image_width = $max_width;
$image_height = round($max_height / $prop);
}
else {
$prop = ($image_height / $image_width);
$image_height = $max_height;
$image_width = round($max_width / $prop);
}
}
echo "<img src=\"\" width=\"$image_width\" height=\"$image_height\" />";
?>
Kurtis
teammatt3 posted this at 05:41 — 7th June 2008.
He has: 2,102 posts
Joined: Sep 2003
You are awesome. Thanks for the code mscreashuns! That works perfectly.
There is no need for a loop because your code gets the height and width assigned a value within the bounds in one pass. The only reason I put a loop there was because I thought I would have to make multiple passes and keep subtracting until the size was in bounds. (I return earlier in the code if the image is already small enough).
Thanks!
kazimmerman posted this at 18:40 — 7th June 2008.
He has: 698 posts
Joined: Jul 2005
Yeah, I forgot to throw out that while() loop. I'd finish off with something like this.
<?php
$imagesize = getimagesize(""); //Path to image
$image_width = $imagesize[0]; //Width of image
$image_height = $imagesize[1]; //Height of image
$max_width = ""; //These two need to be defined.
$max_height = "";
if ($image_width > $image_height) {
/*Now find the proportion of width to height and use it to determine the new height. The new width will be the max width.*/
$prop = ($image_width / $image_height);
$image_width = $max_width;
$image_height = round($max_height / $prop);
}
elseif ($image_height > $image_width) {
$prop = ($image_height / $image_width);
$image_height = $max_height;
$image_width = round($max_width / $prop);
}
else {
$image_width = $max_width;
$image_height = $max_height;
}
echo "<img src=\"\" width=\"$image_width\" height=\"$image_height\" />";
?>
Kurtis
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.