From 093f16f3502195df33c80b8cf086d8d9b2b85f4d Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 18 Feb 2022 19:46:09 +0200 Subject: [PATCH] Improve ImageOperationsTrait & co --- system/src/Grav/Framework/Image/Image.php | 24 ++++++++++++------- .../Image/Traits/ImageOperationsTrait.php | 10 ++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/system/src/Grav/Framework/Image/Image.php b/system/src/Grav/Framework/Image/Image.php index 75ef4025a..d82d8bf97 100644 --- a/system/src/Grav/Framework/Image/Image.php +++ b/system/src/Grav/Framework/Image/Image.php @@ -15,10 +15,16 @@ class Image implements ImageOperationsInterface use ImageOperationsTrait; use Serializable; + /** @var int */ + protected $origWidth; + /** @var int */ + protected $origHeight; /** @var string */ protected $filepath; - /** @var array */ - protected $info; + /** @var int */ + protected $modified; + /** @var int */ + protected $size; /** * @param string $filepath @@ -27,8 +33,8 @@ class Image implements ImageOperationsInterface public function __construct(string $filepath, array $info) { $this->filepath = $filepath; - $this->width = $info['width'] ?? 0; - $this->height = $info['height'] ?? 0; + $this->origWidth = $this->width = $info['width'] ?? 0; + $this->origHeight = $this->height = $info['height'] ?? 0; $this->orientation = isset($info['exif']['Orientation']) ? (int)$info['exif']['Orientation'] : null; } @@ -40,10 +46,11 @@ class Image implements ImageOperationsInterface return [ 'image' => 1, 'filepath' => $this->filepath, - 'info' => $this->info, + 'orientation' => $this->orientation, + 'orig_width' => $this->origWidth, + 'orig_height' => $this->origHeight, 'width' => $this->width, 'height' => $this->height, - 'orientation' => $this->orientation, 'operations' => $this->operations, ]; } @@ -60,10 +67,11 @@ class Image implements ImageOperationsInterface } $this->filepath = $data['filepath']; - $this->info = $data['info']; + $this->origWidth = $data['orig_width']; + $this->origHeight = $data['orig_height']; + $this->orientation = $data['orientation']; $this->width = $data['width']; $this->height = $data['height']; - $this->orientation = $data['orientation']; $this->operations = $data['operations']; } diff --git a/system/src/Grav/Framework/Image/Traits/ImageOperationsTrait.php b/system/src/Grav/Framework/Image/Traits/ImageOperationsTrait.php index 3ec54f3cd..8bb7dc87d 100644 --- a/system/src/Grav/Framework/Image/Traits/ImageOperationsTrait.php +++ b/system/src/Grav/Framework/Image/Traits/ImageOperationsTrait.php @@ -145,6 +145,11 @@ trait ImageOperationsTrait $height = $new_height; } + if ($width === $new_width && $height === $new_height) { + // Nothing to resize. + return $this; + } + $this->operations[] = ['resize', [$bg, $width, $height, $new_width, $new_height]]; // Update image size. @@ -261,6 +266,11 @@ trait ImageOperationsTrait */ public function crop(int $x, int $y, int $width, int $height) { + if ($x === 0 && $y === 0 && $width === $this->width && $height === $this->height) { + // Nothing to crop. + return $this; + } + $this->operations[] = ['crop', [$x, $y, $width, $height]]; // Update image size.