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 1024x768 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.
to get the aspect ratio just simplify the width and height like a fraction for example:
1024 4
---- = ---
768 3
the php code:
function gcd($a, $b)
{
if ($a == 0 || $b == 0)
return abs( max(abs($a), abs($b)) );
$r = $a % $b;
return ($r != 0) ?
gcd($b, $r) :
abs($b);
}
$gcd=gcd(1024,768);
echo "Aspect ratio = ". (1024/$gcd) . ":" . (768/$gcd);
If you can get one of: height, width then you can calculate the missing width height:
original width * new height / original height = new width;
original height * new width / original width = new height;
Or if you just want a ratio:
original width / original height = ratio
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With