From 2d2843c37f98cdb23d54c12319179e8c40442526 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Tue, 9 Jan 2024 09:32:54 +0100 Subject: [PATCH] Incorporate pivot point in CropModifier --- src/Drivers/Gd/Modifiers/CropModifier.php | 24 ++++++++++--------- .../Imagick/Modifiers/CropModifier.php | 20 ++++++++-------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/Drivers/Gd/Modifiers/CropModifier.php b/src/Drivers/Gd/Modifiers/CropModifier.php index c97e11e0..7cac004c 100644 --- a/src/Drivers/Gd/Modifiers/CropModifier.php +++ b/src/Drivers/Gd/Modifiers/CropModifier.php @@ -64,12 +64,14 @@ class CropModifier extends SpecializedModifier $targetHeight ); + // don't alpha blend for covering areas imagealphablending($modified, false); + // cover the possible newly created areas with background color if ($resizeTo->width() > $originalSize->width() || $this->offset_x > 0) { imagefilledrectangle( $modified, - $originalSize->width() + ($this->offset_x * -1), + $originalSize->width() + ($this->offset_x * -1) - $resizeTo->pivot()->x(), 0, $resizeTo->width(), $resizeTo->height(), @@ -78,37 +80,37 @@ class CropModifier extends SpecializedModifier } // cover the possible newly created areas with background color - if ($resizeTo->width() > $originalSize->height() || $this->offset_y > 0) { + if ($resizeTo->height() > $originalSize->height() || $this->offset_y > 0) { imagefilledrectangle( $modified, - ($this->offset_x * -1), - $originalSize->height() + ($this->offset_y * -1), - ($this->offset_x * -1) + $originalSize->width() - 1, + ($this->offset_x * -1) - $resizeTo->pivot()->x(), + $originalSize->height() + ($this->offset_y * -1) - $resizeTo->pivot()->y(), + ($this->offset_x * -1) + $originalSize->width() - 1 - $resizeTo->pivot()->x(), $resizeTo->height(), $background ); } // cover the possible newly created areas with background color - if ($this->offset_x < 0) { + if ($this->offset_x < 0 || $resizeTo->pivot()->x() != 0) { imagefilledrectangle( $modified, 0, 0, - ($this->offset_x * -1) - 1, + ($this->offset_x * -1) - $resizeTo->pivot()->x() - 1, $resizeTo->height(), $background ); } // cover the possible newly created areas with background color - if ($this->offset_y < 0) { + if ($this->offset_y < 0 || $resizeTo->pivot()->y() != 0) { imagefilledrectangle( $modified, - $this->offset_x * -1, + ($this->offset_x * -1) - $resizeTo->pivot()->x(), 0, - ($this->offset_x * -1) + $originalSize->width() - 1, - ($this->offset_y * -1) - 1, + ($this->offset_x * -1) + $originalSize->width() - $resizeTo->pivot()->x() - 1, + ($this->offset_y * -1) - $resizeTo->pivot()->y() - 1, $background ); } diff --git a/src/Drivers/Imagick/Modifiers/CropModifier.php b/src/Drivers/Imagick/Modifiers/CropModifier.php index 16242a29..c351b4bb 100644 --- a/src/Drivers/Imagick/Modifiers/CropModifier.php +++ b/src/Drivers/Imagick/Modifiers/CropModifier.php @@ -46,7 +46,7 @@ class CropModifier extends DriverSpecializedModifier // cover the possible newly created areas with background color if ($crop->width() > $originalSize->width() || $this->offset_x > 0) { $draw->rectangle( - $originalSize->width() + ($this->offset_x * -1), + $originalSize->width() + ($this->offset_x * -1) - $crop->pivot()->x(), 0, $crop->width(), $crop->height() @@ -56,30 +56,30 @@ class CropModifier extends DriverSpecializedModifier // cover the possible newly created areas with background color if ($crop->height() > $originalSize->height() || $this->offset_y > 0) { $draw->rectangle( - ($this->offset_x * -1), - $originalSize->height() + ($this->offset_y * -1), - ($this->offset_x * -1) + $originalSize->width() - 1, + ($this->offset_x * -1) - $crop->pivot()->x(), + $originalSize->height() + ($this->offset_y * -1) - $crop->pivot()->y(), + ($this->offset_x * -1) + $originalSize->width() - 1 - $crop->pivot()->x(), $crop->height() ); } // cover the possible newly created areas with background color - if ($this->offset_x < 0) { + if ($this->offset_x < 0 || $crop->pivot()->x() != 0) { $draw->rectangle( 0, 0, - ($this->offset_x * -1) - 1, + ($this->offset_x * -1) - $crop->pivot()->x() - 1, $crop->height() ); } // cover the possible newly created areas with background color - if ($this->offset_y < 0) { + if ($this->offset_y < 0 || $crop->pivot()->y() != 0) { $draw->rectangle( - $this->offset_x * -1, + ($this->offset_x * -1) - $crop->pivot()->x(), 0, - ($this->offset_x * -1) + $originalSize->width() - 1, - ($this->offset_y * -1) - 1, + ($this->offset_x * -1) + $originalSize->width() - $crop->pivot()->x() - 1, + ($this->offset_y * -1) - $crop->pivot()->y() - 1, ); }