Skip to content

Instantly share code, notes, and snippets.

@chrisdempsey
Forked from wazum/aspect_ratio.php
Created February 24, 2021 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdempsey/412d944781436977fef6e6a0ffe79d23 to your computer and use it in GitHub Desktop.
Save chrisdempsey/412d944781436977fef6e6a0ffe79d23 to your computer and use it in GitHub Desktop.
PHP Calculate aspect ratio from width and height
<?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