diff --git a/src/Canvas.php b/src/Canvas.php index f72b30e..eacef13 100644 --- a/src/Canvas.php +++ b/src/Canvas.php @@ -26,17 +26,6 @@ class Canvas { } } - public function __destruct() { - $this->destroy(); - } - - public function destroy() : self - { - \is_resource($this->image) && \imagedestroy($this->image); - - return $this; - } - public function create(int $width, int $height) : self { $this->width = $width; @@ -200,8 +189,18 @@ class Canvas { unset($src->image); } - public function constrain(int $width, int $height, bool $immutable = true) : self + public function constrain(? int $width, ? int $height, bool $immutable = true) : self { + if ($width === null && $height === null) { + throw new \LogicException("At least one limiting factor must be provided, either width or height (or both)."); + } + elseif ($width === null) { + $width = $this->width * ( $height / $this->height ); + } + elseif ($height === null) { + $height = $this->height * ( $width / $this->width ); + } + if ( ($this->width > $width) || ($this->height > $height) ) { if ($this->width > $this->height) { $height = $this->height * ( $width / $this->width ); @@ -224,6 +223,7 @@ class Canvas { } } + return $this; } diff --git a/src/Image.php b/src/Image.php index 3fe4e8e..46a21c3 100644 --- a/src/Image.php +++ b/src/Image.php @@ -120,12 +120,12 @@ class Image extends Canvas { return $img; } - public function save(int $quality = null, $filters = null) : bool + public function save(? int $quality = null, $filters = null) : bool { return $this->render($this->filename, $quality, $filters); } - public function render(string $path = null, int $quality = null, $filters = null) # : bool + public function render(? string $path = null, ? int $quality = null, $filters = null) # : bool { $layers = $this->layers();