From 6308afbd3e3af910980efcb4c2d4f8f718f0fdf1 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 7 Jan 2024 15:06:55 +0100 Subject: [PATCH] Display the blending color in GD's CropModifier --- src/Drivers/Gd/Modifiers/CropModifier.php | 27 ++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Drivers/Gd/Modifiers/CropModifier.php b/src/Drivers/Gd/Modifiers/CropModifier.php index 60d9c323..4132a080 100644 --- a/src/Drivers/Gd/Modifiers/CropModifier.php +++ b/src/Drivers/Gd/Modifiers/CropModifier.php @@ -17,32 +17,43 @@ class CropModifier extends SpecializedModifier { public function apply(ImageInterface $image): ImageInterface { + $originalSize = $image->size(); $crop = $this->crop($image); foreach ($image as $frame) { - $this->cropFrame($frame, $crop); + $this->cropFrame($frame, $originalSize, $crop); } return $image; } - protected function cropFrame(FrameInterface $frame, SizeInterface $resizeTo): void + protected function cropFrame(FrameInterface $frame, SizeInterface $originalSize, SizeInterface $resizeTo): void { // create new image $modified = Cloner::cloneEmpty($frame->native(), $resizeTo); + // define offset + $offset_x = ($resizeTo->pivot()->x() + $this->offset_x); + $offset_y = ($resizeTo->pivot()->y() + $this->offset_y); + + // define target width & height + $targetWidth = min($resizeTo->width(), $originalSize->width()); + $targetHeight = min($resizeTo->height(), $originalSize->height()); + $targetWidth = $targetWidth < $originalSize->width() ? $targetWidth + $offset_x : $targetWidth; + $targetHeight = $targetHeight < $originalSize->height() ? $targetHeight + $offset_y : $targetHeight; + // copy content from resource imagecopyresampled( $modified, $frame->native(), + $offset_x * -1, + $offset_y * -1, 0, 0, - $resizeTo->pivot()->x() + $this->offset_x, - $resizeTo->pivot()->y() + $this->offset_y, - $resizeTo->width(), - $resizeTo->height(), - $resizeTo->width(), - $resizeTo->height(), + $targetWidth, + $targetHeight, + $targetWidth, + $targetHeight ); // set new content as recource