I can’t believe I can’t find the formula for this. I am using a PHP script called SLIR to resize images. The script asks to specify an aspect ratio for cropping. I’d like to get the aspect ratio based on the width and height of the image being specified in a form I’m letting users enter these values in. For example, if a user enters a 1024×768 image, I would get an aspect ratio of 4:3. For the life of me, I can’t find an example of the formula in PHP or Javascript I can use to get the aspect ratio values based on knowing the w,h and plug the aspect ratio into a variable.
Share
There is no need for you to do any kind of calculation.
Just because it says aspect ratio doesn’t mean it has to be one of a limited set of commonly used ratios. It can be any pair of numbers separated by a colon.
Quoting from the SLIR usage guide:
Note that they didn’t bother to reduce that even further to
c3:2.So, simply use the values as entered by the user:
1024:768.If you want to be concise, calculate the greatest common divisor of the width and height and divide both of them by that. That would reduce your
1024:768down to4:3.