-
-
Save newactual/f7e7800d0a896296325ac084656861fd to your computer and use it in GitHub Desktop.
PHP Calculate aspect ratio from width and height
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getAspectRatio(int $width, int $height) | |
{ | |
// search for greatest common divisor | |
$greatestCommonDivisor = static function($width, $height) use (&$greatestCommonDivisor) { | |
return ($width % $height) ? $greatestCommonDivisor($height, $width % $height) : $height; | |
}; | |
$divisor = $greatestCommonDivisor($width, $height); | |
return $width / $divisor . ':' . $height / $divisor; | |
} | |
echo getAspectRatio(1280, 1024); | |
echo PHP_EOL; | |
echo getAspectRatio(1275, 715); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment