Oihana PHP

aspectRatio.php

Table of Contents

Functions

aspectRatio()  : string
Returns the aspect ratio of a width and a height, simplified by their greatest common divisor, e.g. `"16:9"` for 1920 × 1080.

Functions

aspectRatio()

Returns the aspect ratio of a width and a height, simplified by their greatest common divisor, e.g. `"16:9"` for 1920 × 1080.

aspectRatio(int $width, int $height[, string $separator = ':' ]) : string

The two numbers are divided by gcd(): 1920 × 1080 and 1280 × 720 both give "16:9", 1280 × 1080 gives "32:27". The separator defaults to a colon and can be changed ("16/9").

A width or a height of zero or below has no ratio — and a negative one would be read as its absolute value by gcd(), hiding an error upstream: both throw rather than return a string a screen would show without anyone knowing why.

To scale a size while keeping its ratio, see aspectFit().

Parameters
$width : int

The width, greater than 0.

$height : int

The height, greater than 0.

$separator : string = ':'

The string written between the two numbers (default ':').

Tags
throws
InvalidArgumentException

When the width or the height is zero or below.

example
use function oihana\core\maths\aspectRatio ;

echo aspectRatio( 1920 , 1080 ) ;        // "16:9"
echo aspectRatio( 1280 , 1080 ) ;        // "32:27"
echo aspectRatio( 1920 , 1080 , '/' ) ;  // "16/9"
echo aspectRatio( 1080 , 1080 ) ;        // "1:1"
author

Marc Alcaraz (ekameleon)

since
1.3.0
Return values
string —

The simplified ratio, e.g. "16:9".

On this page

Search results