Oihana PHP System

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
author

Oihana

version
1.0.0
example
<?php

// Create a new unlocked aspect ratio for a Full HD resolution (1920x1080)
$ratio = new AspectRatio(1920, 1080);

echo "Initial Width: " . $ratio->getWidth() . "\n";      // 1920
echo "Initial Height: " . $ratio->getHeight() . "\n";     // 1080
echo "GCD: " . $ratio->getGCD() . "\n";                    // 120
// The simplified aspect ratio is 16:9 (1920/120 = 16, 1080/120 = 9)

// Since it's unlocked, changing the width will change the ratio
$ratio->setWidth(1280);
echo "New Width: " . $ratio->getWidth() . "\n";        // 1280
echo "Height is unchanged: " . $ratio->getHeight() . "\n"; // 1080 (Ratio is now different)

// ---

// Now, create a locked aspect ratio
$lockedRatio = new AspectRatio(1920, 1080, true);
echo "Locked Ratio Width: " . $lockedRatio->getWidth() . "\n";  // 1920
echo "Locked Ratio Height: " . $lockedRatio->getHeight() . "\n"; // 1080

// Change the width. The height will be automatically adjusted to maintain 16:9
$lockedRatio->setWidth(1280);
echo "New Locked Width: " . $lockedRatio->getWidth() . "\n";    // 1280
echo "New Locked Height: " . $lockedRatio->getHeight() . "\n"; // 720 (1280 * 9 / 16)

// Lock an existing ratio and change the height
$ratio->lock();
$ratio->setHeight(900);
echo "Manually Locked Width: " . $ratio->getWidth() . "\n";  // 1600 (Recalculated from 1280x900 ratio)
echo "Manually Locked Height: " . $ratio->getHeight() . "\n"; // 900

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

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.


        
On this page

Search results