Skip to content

Instantly share code, notes, and snippets.

@richaber
Created September 15, 2022 21:34
Show Gist options
  • Save richaber/0e679bb8935a474b3bb197d55bdd05ac to your computer and use it in GitHub Desktop.
Save richaber/0e679bb8935a474b3bb197d55bdd05ac to your computer and use it in GitHub Desktop.
PHP Aspect Ratio Utility
<?php
namespace App\Utility;
class AspectRatioUtility
{
/**
* Get the aspect ratio.
*
* @param int $width The width.
* @param int $height The height.
*
* @return string
*/
public static function getAspectRatio(int $width, int $height): string
{
$gcd = self::getGreatestCommonDivisor($width, $height);
return (string)($width / $gcd) . ':' . ($height / $gcd);
}
/**
* Returns the greatest common divisor of two integers using the Euclidean algorithm.
*
* @param int $a
* @param int $b
*
* @return int
*/
public static function getGreatestCommonDivisor(int $a, int $b): int
{
return (int)($a % $b) ? self::getGreatestCommonDivisor($b, $a % $b) : $b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment