Oihana PHP

aspectFit.php

Table of Contents

Functions

aspectFit()  : array{width: int, height: int}
Scales a dimension pair to a target width or height while preserving the original aspect ratio (the locked-ratio behaviour of an aspect-ratio box).

Functions

aspectFit()

Scales a dimension pair to a target width or height while preserving the original aspect ratio (the locked-ratio behaviour of an aspect-ratio box).

aspectFit(int $width, int $height[, int|null $targetWidth = null ][, int|null $targetHeight = null ]) : array{width: int, height: int}

Provide $targetWidth to derive the matching height, or $targetHeight to derive the matching width. If both are given, $targetWidth takes precedence and $targetHeight is ignored. If both are null, the original pair is returned. A non-positive original $width/$height yields an undefined ratio: the provided targets (or the originals) are returned unchanged.

Parameters
$width : int

The original (reference) width.

$height : int

The original (reference) height.

$targetWidth : int|null = null

The desired width, or null.

$targetHeight : int|null = null

The desired height, or null.

Tags
example
use function oihana\core\maths\aspectFit ;

aspectFit( 1920 , 1080 , targetWidth: 1280 ) ; // [ 'width' => 1280 , 'height' => 720 ]
aspectFit( 1920 , 1080 , targetHeight: 540 ) ; // [ 'width' => 960  , 'height' => 540 ]
author

Marc Alcaraz (ekameleon)

since
1.2.0
Return values
array{width: int, height: int}

The scaled dimensions preserving the ratio.

On this page

Search results