# If(\$ratio1>\$ratio2, How Do You Know?

## Recommended Posts

I have an image upload and resize function... I would like to know the proper ratio information to help resize the image and keep the proportions or aspect ratio.How does this

` work or how should it work? [code]\$old_x=imagesx(\$src_img); //width\$old_y=imagesy(\$src_img); //height\$ratio1=\$old_x/\$new_h;\$ratio2=\$old_y/\$new_w;if(\$ratio1>\$ratio2){\$thumb_w=\$new_w;\$thumb_h=\$old_y/\$ratio1;}else{\$thumb_h=\$new_h;\$thumb_w=\$old_x/\$ratio2;}`

##### Share on other sites

In a coordinate system, x is left-right and y is up-down. I assume h is height and w is width, so h is up-down (Y) and w is left-right (X).I would say to divide the original picture's height and width (smaller_value/bigger_value), store it in a variable. Set a height and/or width that you want to be the maximum for the thumbnail, and then multiply the maximum number you set to the variable you set with the value of the height/width.

##### Share on other sites

The ratio of an image is the width divided by the height. If you have an image that is 800x600, or a 4:3 aspect ratio, the width divided by the height will be 1.33. If you want to resize the image to a max of 250 px wide, for example, so you're converting the 800 to 250, then you divide 250 by 800 to get the ratio that it was resized. You multiply the height by that same ratio to get the new height, in this case 187 px high.

##### Share on other sites

I worked out this below... there isn't a 187px ?

`echo "<strong>Real Width and Height</strong><br />";\$img_width = 800; \$img_height = 600;echo "img_width = ".\$img_width."<br />";echo "img_height = ".\$img_height."<br />";echo "<hr />";//-----------echo "<strong>Set the width and height needed</strong><br />";\$set_width = 250;\$set_height = 250;echo "set_width = ".\$set_width."<br />";echo "set_height = ".\$set_height."<br />";echo "<hr />";//-----------echo "<strong>Work out your ratio needed for a properly resized image</strong><br />";echo "<br />";\$ratio1 = \$set_width / \$img_width;echo "<strong>ratio1 = set_width / img_width = ratio1</strong><br />";echo "\$set_width / \$img_width = \$ratio1<br />";echo "ratio1 = \$ratio1<br />";echo "<strong>The NEW height calculation:</strong><br />"; echo "<strong>new_height = img_height * ratio1 = new_height</strong><br />";\$new_height = \$img_height * \$ratio1;echo "This height will be: <strong>\$new_height</strong><br />";echo "<hr />";`

Edited by student101
##### Share on other sites

If you have the ratio of the new height to the old height, you can use that ratio to calculate the new width based on the old width. It doesn't matter which dimension you start with, if you get the resize ratio you can use that to calculate the other dimension.

##### Share on other sites
If you have the ratio of the new height to the old height, you can use that ratio to calculate the new width based on the old width. It doesn't matter which dimension you start with, if you get the resize ratio you can use that to calculate the other dimension.
Ok cool, any image resized becomes 250px in width... Unless this code is incorrect?
`echo "<strong>Work out your WIDTH ratio needed for a properly resized image</strong><br />";echo "<br />";\$ratio2 = \$set_height / \$img_height;echo "<strong>ratio2 = set_height / img_height = ratio2</strong><br />";echo "\$set_height / \$img_height = \$ratio2<br />";echo "ratio2 = \$ratio2<br />";echo "<strong>The NEW width calculation:</strong><br />"; echo "<strong>new_width = img_height * ratio2 = new_width</strong><br />";\$new_width = \$img_height * \$ratio2;echo "This width will be: <strong>\$new_width</strong><br />";echo "<hr />";`

Edited by student101
##### Share on other sites

I'm not sure what \$new_height is, isn't the new height the value of \$set_height? The width is what you're trying to calculate, you already know what the height is (250). You need to use the ratio of the new height to the old height to calculate the new width based on the old width.

##### Share on other sites

Fixed, I think...

This Forum has some serious issues, regarding cookies / sessions or whatever keeps me logged in.I need to log in for every page including every click!!! Every timeMan this is just irritating, does it add a cookie or not? Does it know that it added a cookie?Please could the creators / developers fix the login / session / cookie issues....
WinXP SP3, Latest Firefox, Latest IE, Latest Opera.THANK YOU, for your time and your explanations.I do appreciate it.Cheers Edited by student101

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.