AspectRatio
Manages and enforces an aspect ratio for a given width and height.
This class can represent dimensions and, when locked, will automatically adjust one dimension when the other is changed to maintain the original aspect ratio.
When unlocked, changing a dimension will result in a new aspect ratio being calculated.
Tags
Table of Contents
Properties
- $gcd : int
- Gets the Greatest Common Divisor (GCD) of the current width and height.
- $height : int
- The current height size.
- $width : int
- The current width size.
- $_height : int
- $_width : int
- $_aspH : int
- $_aspW : int
- $_gcd : int
- $_locked : bool
Methods
- __construct() : mixed
- AspectRatio constructor.
- isLocked() : bool
- Checks if the aspect ratio is currently locked.
- lock() : void
- Locks the aspect ratio.
- unlock() : void
- Unlocks the aspect ratio.
- recalculateGCD() : void
- Recalculates the GCD and the simplified aspect ratio components.
Properties
$gcd read-only virtual
Gets the Greatest Common Divisor (GCD) of the current width and height.
public
int
$gcd
This calculation casts the width and height to integers, ignoring any floating-point values.
Hooks
public
int
get
$height virtual
The current height size.
public
int
$height
If the object is locked, the width is automatically adjusted to maintain the aspect ratio. If unlocked, the aspect ratio is recalculated based on the new height and existing width.
Hooks
public
int
get
public
set
$width virtual
The current width size.
public
int
$width
If the object is locked, the height is automatically adjusted to maintain the aspect ratio. If unlocked, the aspect ratio is recalculated based on the new width and existing height.
Hooks
public
int
get
public
set
$_height
protected
int
$_height
$_width
protected
int
$_width
$_aspH
private
int
$_aspH
$_aspW
private
int
$_aspW
$_gcd
private
int
$_gcd
$_locked
private
bool
$_locked
Methods
__construct()
AspectRatio constructor.
public
__construct([float|int $width = 0 ][, float|int $height = 0 ][, bool $lock = false ]) : mixed
Initializes the aspect ratio with a given width and height.
Can be immediately locked to enforce the calculated ratio.
Parameters
- $width : float|int = 0
-
The initial width.
- $height : float|int = 0
-
The initial height.
- $lock : bool = false
-
If true, the aspect ratio is locked upon creation.
isLocked()
Checks if the aspect ratio is currently locked.
public
isLocked() : bool
Return values
bool —True if the aspect ratio is locked, false otherwise.
lock()
Locks the aspect ratio.
public
lock() : void
After calling this method, any subsequent changes to width or height will proportionally adjust the other dimension to maintain the current ratio.
unlock()
Unlocks the aspect ratio.
public
unlock() : void
After calling this method, changing the width or height will no longer affect the other dimension, and the ratio will be recalculated instead.
recalculateGCD()
Recalculates the GCD and the simplified aspect ratio components.
private
recalculateGCD() : void
This private method is called during initialization and whenever a dimension is changed while the object is in an unlocked state.