Skip to content

Instantly share code, notes, and snippets.

@newactual
Forked from wazum/aspect_ratio.php
Created January 10, 2023 11:19
Show Gist options
  • Save newactual/f7e7800d0a896296325ac084656861fd to your computer and use it in GitHub Desktop.
Save newactual/f7e7800d0a896296325ac084656861fd 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