Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,10 @@ | |
class RectangleResizer | ||
{ | ||
/** | ||
* @param null|int $width | ||
* @param null|int $height | ||
* @throws GeometryException | ||
* @return void | ||
*/ | ||
public function __construct( | ||
protected ?int $width = null, | ||
|
@@ -31,35 +34,61 @@ public function __construct( | |
} | ||
|
||
/** | ||
* @throws GeometryException | ||
* Static factory method to create resizer with given target size | ||
* | ||
* @param array $arguments | ||
* @return RectangleResizer | ||
*/ | ||
public static function to(mixed ...$arguments): self | ||
Check failure on line 42 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.1 - prefer-stable - ImageMagick 6.9.12-55
Check failure on line 42 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.1 - prefer-stable - ImageMagick 7.1.0-40
Check failure on line 42 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.2 - prefer-stable - ImageMagick 6.9.12-55
Check failure on line 42 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.2 - prefer-stable - ImageMagick 7.1.0-40
Check failure on line 42 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.3 - prefer-stable - ImageMagick 6.9.12-55
Check failure on line 42 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.3 - prefer-stable - ImageMagick 7.1.0-40
|
||
{ | ||
return new self(...$arguments); | ||
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.1 - prefer-stable - ImageMagick 6.9.12-55
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.1 - prefer-stable - ImageMagick 6.9.12-55
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.1 - prefer-stable - ImageMagick 7.1.0-40
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.1 - prefer-stable - ImageMagick 7.1.0-40
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.2 - prefer-stable - ImageMagick 6.9.12-55
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.2 - prefer-stable - ImageMagick 6.9.12-55
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.2 - prefer-stable - ImageMagick 7.1.0-40
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.2 - prefer-stable - ImageMagick 7.1.0-40
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.3 - prefer-stable - ImageMagick 6.9.12-55
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.3 - prefer-stable - ImageMagick 6.9.12-55
Check failure on line 44 in src/Geometry/Tools/RectangleResizer.php GitHub Actions / PHP 8.3 - prefer-stable - ImageMagick 7.1.0-40
|
||
} | ||
|
||
/** | ||
* Determine if resize has target width | ||
* | ||
* @return bool | ||
*/ | ||
protected function hasTargetWidth(): bool | ||
{ | ||
return is_integer($this->width); | ||
} | ||
|
||
/** | ||
* Return target width of resizer if available | ||
* | ||
* @return null|int | ||
*/ | ||
protected function getTargetWidth(): ?int | ||
{ | ||
return $this->hasTargetWidth() ? $this->width : null; | ||
} | ||
|
||
/** | ||
* Determine if resize has target height | ||
* | ||
* @return bool | ||
*/ | ||
protected function hasTargetHeight(): bool | ||
{ | ||
return is_integer($this->height); | ||
} | ||
|
||
/** | ||
* Return target width of resizer if available | ||
* | ||
* @return null|int | ||
*/ | ||
protected function getTargetHeight(): ?int | ||
{ | ||
return $this->hasTargetHeight() ? $this->height : null; | ||
} | ||
|
||
/** | ||
* Return target size object | ||
* | ||
* @throws GeometryException | ||
* @return SizeInterface | ||
*/ | ||
protected function getTargetSize(): SizeInterface | ||
{ | ||
|
@@ -70,20 +99,38 @@ protected function getTargetSize(): SizeInterface | |
return new Rectangle($this->width, $this->height); | ||
} | ||
|
||
/** | ||
* Set target width of resizer | ||
* | ||
* @param int $width | ||
* @return RectangleResizer | ||
*/ | ||
public function toWidth(int $width): self | ||
{ | ||
$this->width = $width; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set target height of resizer | ||
* | ||
* @param int $height | ||
* @return RectangleResizer | ||
*/ | ||
public function toHeight(int $height): self | ||
{ | ||
$this->height = $height; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set target size to given size object | ||
* | ||
* @param SizeInterface $size | ||
* @return RectangleResizer | ||
*/ | ||
public function toSize(SizeInterface $size): self | ||
{ | ||
$this->width = $size->width(); | ||
|
@@ -92,6 +139,12 @@ public function toSize(SizeInterface $size): self | |
return $this; | ||
} | ||
|
||
/** | ||
* Get proportinal width | ||
* | ||
* @param SizeInterface $size | ||
* @return int | ||
*/ | ||
protected function getProportionalWidth(SizeInterface $size): int | ||
{ | ||
if (!$this->hasTargetHeight()) { | ||
|
@@ -101,6 +154,12 @@ protected function getProportionalWidth(SizeInterface $size): int | |
return max([1, (int) round($this->height * $size->aspectRatio())]); | ||
} | ||
|
||
/** | ||
* Get proportinal height | ||
* | ||
* @param SizeInterface $size | ||
* @return int | ||
*/ | ||
protected function getProportionalHeight(SizeInterface $size): int | ||
{ | ||
if (!$this->hasTargetWidth()) { | ||
|
@@ -110,6 +169,12 @@ protected function getProportionalHeight(SizeInterface $size): int | |
return max([1, (int) round($this->width / $size->aspectRatio())]); | ||
} | ||
|
||
/** | ||
* Resize given size to target size of the resizer | ||
* | ||
* @param SizeInterface $size | ||
* @return SizeInterface | ||
*/ | ||
public function resize(SizeInterface $size): SizeInterface | ||
{ | ||
$resized = new Rectangle($size->width(), $size->height()); | ||
|
@@ -125,6 +190,12 @@ public function resize(SizeInterface $size): SizeInterface | |
return $resized; | ||
} | ||
|
||
/** | ||
* Resize given size to target size of the resizer but do not exceed original size | ||
* | ||
* @param SizeInterface $size | ||
* @return SizeInterface | ||
*/ | ||
public function resizeDown(SizeInterface $size): SizeInterface | ||
{ | ||
$resized = new Rectangle($size->width(), $size->height()); | ||
|
@@ -144,6 +215,12 @@ public function resizeDown(SizeInterface $size): SizeInterface | |
return $resized; | ||
} | ||
|
||
/** | ||
* Resize given size to target size proportinally | ||
* | ||
* @param SizeInterface $size | ||
* @return SizeInterface | ||
*/ | ||
public function scale(SizeInterface $size): SizeInterface | ||
{ | ||
$resized = new Rectangle($size->width(), $size->height()); | ||
|
@@ -168,6 +245,12 @@ public function scale(SizeInterface $size): SizeInterface | |
return $resized; | ||
} | ||
|
||
/** | ||
* Resize given size to target size proportinally but do not exceed original size | ||
* | ||
* @param SizeInterface $size | ||
* @return SizeInterface | ||
*/ | ||
public function scaleDown(SizeInterface $size): SizeInterface | ||
{ | ||
$resized = new Rectangle($size->width(), $size->height()); | ||
|