1
0
mirror of https://github.com/Intervention/image.git synced 2025-09-03 10:53:01 +02:00

Fix issues with newly created areas in CropModifier

This commit is contained in:
Oliver Vogel
2024-01-08 14:51:28 +01:00
parent 32ee26ff49
commit 614cd93517
2 changed files with 62 additions and 37 deletions

View File

@@ -2,12 +2,8 @@
namespace Intervention\Image\Drivers\Gd\Modifiers; namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\Drivers\Gd\SpecializedModifier; use Intervention\Image\Drivers\Gd\SpecializedModifier;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FrameInterface; use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface; use Intervention\Image\Interfaces\SizeInterface;
@@ -24,7 +20,9 @@ class CropModifier extends SpecializedModifier
{ {
$originalSize = $image->size(); $originalSize = $image->size();
$crop = $this->crop($image); $crop = $this->crop($image);
$background = $this->driver()->handleInput($this->background); $background = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->background)
);
foreach ($image as $frame) { foreach ($image as $frame) {
$this->cropFrame($frame, $originalSize, $crop, $background); $this->cropFrame($frame, $originalSize, $crop, $background);
@@ -37,10 +35,10 @@ class CropModifier extends SpecializedModifier
FrameInterface $frame, FrameInterface $frame,
SizeInterface $originalSize, SizeInterface $originalSize,
SizeInterface $resizeTo, SizeInterface $resizeTo,
ColorInterface $background int $background
): void { ): void {
// create new image // create new image with transparent background
$modified = Cloner::cloneEmpty($frame->native(), $resizeTo, $background); $modified = Cloner::cloneEmpty($frame->native(), $resizeTo);
// define offset // define offset
$offset_x = ($resizeTo->pivot()->x() + $this->offset_x); $offset_x = ($resizeTo->pivot()->x() + $this->offset_x);
@@ -52,26 +50,6 @@ class CropModifier extends SpecializedModifier
$targetWidth = $targetWidth < $originalSize->width() ? $targetWidth + $offset_x : $targetWidth; $targetWidth = $targetWidth < $originalSize->width() ? $targetWidth + $offset_x : $targetWidth;
$targetHeight = $targetHeight < $originalSize->height() ? $targetHeight + $offset_y : $targetHeight; $targetHeight = $targetHeight < $originalSize->height() ? $targetHeight + $offset_y : $targetHeight;
// make image area transparent to keep transparency
// even if background-color is set
$transparent = imagecolorallocatealpha(
$modified,
$background->channel(Red::class)->value(),
$background->channel(Green::class)->value(),
$background->channel(Blue::class)->value(),
127,
);
imagealphablending($modified, false); // do not blend / just overwrite
imagefilledrectangle(
$modified,
$offset_x * -1,
$offset_y * -1,
$targetWidth - $this->offset_x - 1,
$targetHeight - $this->offset_y - 1,
$transparent
);
// copy content from resource // copy content from resource
imagecopyresampled( imagecopyresampled(
$modified, $modified,
@@ -86,6 +64,55 @@ class CropModifier extends SpecializedModifier
$targetHeight $targetHeight
); );
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),
0,
$resizeTo->width(),
$resizeTo->height(),
$background
);
}
// cover the possible newly created areas with background color
if ($resizeTo->width() > $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,
$resizeTo->height(),
$background
);
}
// cover the possible newly created areas with background color
if ($this->offset_x < 0) {
imagefilledrectangle(
$modified,
0,
0,
($this->offset_x * -1) - 1,
$resizeTo->height(),
$background
);
}
// cover the possible newly created areas with background color
if ($this->offset_y < 0) {
imagefilledrectangle(
$modified,
$this->offset_x * -1,
0,
($this->offset_x * -1) + $originalSize->width() - 1,
($this->offset_y * -1) - 1,
$background
);
}
// set new content as recource // set new content as recource
$frame->setNative($modified); $frame->setNative($modified);
} }

View File

@@ -43,18 +43,16 @@ class CropModifier extends DriverSpecializedModifier
$crop->width(), $crop->width(),
$crop->height() $crop->height()
); );
$frame->native()->drawImage($draw);
} }
// cover the possible newly created areas with background color // cover the possible newly created areas with background color
if ($crop->height() > $originalSize->height() || $this->offset_y > 0) { if ($crop->height() > $originalSize->height() || $this->offset_y > 0) {
$draw->rectangle( $draw->rectangle(
0, ($this->offset_x * -1),
$originalSize->height() + ($this->offset_y * -1), $originalSize->height() + ($this->offset_y * -1),
$crop->width(), ($this->offset_x * -1) + $originalSize->width() - 1,
$crop->height() $crop->height()
); );
$frame->native()->drawImage($draw);
} }
// cover the possible newly created areas with background color // cover the possible newly created areas with background color
@@ -63,21 +61,21 @@ class CropModifier extends DriverSpecializedModifier
0, 0,
0, 0,
($this->offset_x * -1) - 1, ($this->offset_x * -1) - 1,
$originalSize->height() + ($this->offset_y * -1) $crop->height()
); );
$frame->native()->drawImage($draw);
} }
// cover the possible newly created areas with background color // cover the possible newly created areas with background color
if ($this->offset_y < 0) { if ($this->offset_y < 0) {
$draw->rectangle( $draw->rectangle(
$this->offset_x * -1,
0, 0,
0, ($this->offset_x * -1) + $originalSize->width() - 1,
$crop->width(),
($this->offset_y * -1) - 1, ($this->offset_y * -1) - 1,
); );
$frame->native()->drawImage($draw);
} }
$frame->native()->drawImage($draw);
} }
return $image; return $image;