*/ private const STANDARD_ASPECTS = array( array( 'w' => 1, 'h' => 1, 'ratio' => 1.0, ), array( 'w' => 3, 'h' => 2, 'ratio' => 1.5, ), array( 'w' => 2, 'h' => 3, 'ratio' => 0.6667, ), array( 'w' => 4, 'h' => 3, 'ratio' => 1.3333, ), array( 'w' => 3, 'h' => 4, 'ratio' => 0.75, ), array( 'w' => 16, 'h' => 9, 'ratio' => 1.7778, ), array( 'w' => 9, 'h' => 16, 'ratio' => 0.5625, ), ); /** * Snap pixel dimensions to the nearest supported aspect ratio string. * * @param int $width Width in pixels. * @param int $height Height in pixels (must be > 0 to avoid division by zero). * @return string Ratio string e.g. "16:9". Returns "1:1" when height is 0. */ public static function snap( int $width, int $height ): string { $target = $height > 0 ? (float) $width / (float) $height : 1.0; $best = self::STANDARD_ASPECTS[0]; $best_d = abs( $target - $best['ratio'] ); foreach ( self::STANDARD_ASPECTS as $candidate ) { $d = abs( $target - $candidate['ratio'] ); if ( $d < $best_d ) { $best = $candidate; $best_d = $d; } } return $best['w'] . ':' . $best['h']; } }