Skip to content

Commit

Permalink
Fix alpha blending in ResizeCanvasModifier
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Aug 15, 2024
1 parent 3ba445b commit 21cb93d
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Drivers/Gd/Modifiers/ResizeCanvasModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function modify(
SizeInterface $resize,
ColorInterface $background,
): void {
// create new canvas with target size & target background color
// create new canvas with target size & transparent background color
$modified = Cloner::cloneEmpty($frame->native(), $resize, $background);

// make image area transparent to keep transparency
Expand All @@ -56,19 +56,20 @@ protected function modify(
127,
);

imagealphablending($modified, false); // do not blend - just overwrite
// create transparent area to place the original on top
imagealphablending($modified, false); // do not blend / just overwrite
imagecolortransparent($modified, $transparent);
imagefilledrectangle(
$modified,
$resize->pivot()->x() * -1,
$resize->pivot()->y() * -1,
$resize->pivot()->x() * -1 + $frame->size()->width() - 1,
$resize->pivot()->y() * -1 + $frame->size()->height() - 1,
$transparent
abs($resize->pivot()->x()) + $frame->size()->width() - 1,
abs($resize->pivot()->y()) + $frame->size()->height() - 1,
$transparent,
);

// copy image from original with blending alpha
imagealphablending($modified, true);
imagecopyresampled(
// place original
imagecopy(
$modified,
$frame->native(),
$resize->pivot()->x() * -1,
Expand All @@ -77,8 +78,6 @@ protected function modify(
0,
$frame->size()->width(),
$frame->size()->height(),
$frame->size()->width(),
$frame->size()->height()
);

// set new content as resource
Expand Down

0 comments on commit 21cb93d

Please sign in to comment.