Created
September 15, 2022 21:34
-
-
Save richaber/0e679bb8935a474b3bb197d55bdd05ac to your computer and use it in GitHub Desktop.
PHP Aspect Ratio Utility
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 | |
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