-
-
Save chrisdempsey/412d944781436977fef6e6a0ffe79d23 to your computer and use it in GitHub Desktop.
PHP Calculate aspect ratio from width and height
This file contains hidden or 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