1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-29 16:50:07 +02:00

Fix alpha blending in ResizeCanvasModifier

This commit is contained in:
Oliver Vogel
2024-08-15 16:11:05 +02:00
parent 3ba445b2d3
commit 21cb93dcc6

View File

@@ -43,7 +43,7 @@ class ResizeCanvasModifier extends GenericResizeCanvasModifier implements Specia
SizeInterface $resize, SizeInterface $resize,
ColorInterface $background, ColorInterface $background,
): void { ): 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); $modified = Cloner::cloneEmpty($frame->native(), $resize, $background);
// make image area transparent to keep transparency // make image area transparent to keep transparency
@@ -56,19 +56,20 @@ class ResizeCanvasModifier extends GenericResizeCanvasModifier implements Specia
127, 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( imagefilledrectangle(
$modified, $modified,
$resize->pivot()->x() * -1, $resize->pivot()->x() * -1,
$resize->pivot()->y() * -1, $resize->pivot()->y() * -1,
$resize->pivot()->x() * -1 + $frame->size()->width() - 1, abs($resize->pivot()->x()) + $frame->size()->width() - 1,
$resize->pivot()->y() * -1 + $frame->size()->height() - 1, abs($resize->pivot()->y()) + $frame->size()->height() - 1,
$transparent $transparent,
); );
// copy image from original with blending alpha // place original
imagealphablending($modified, true); imagecopy(
imagecopyresampled(
$modified, $modified,
$frame->native(), $frame->native(),
$resize->pivot()->x() * -1, $resize->pivot()->x() * -1,
@@ -77,8 +78,6 @@ class ResizeCanvasModifier extends GenericResizeCanvasModifier implements Specia
0, 0,
$frame->size()->width(), $frame->size()->width(),
$frame->size()->height(), $frame->size()->height(),
$frame->size()->width(),
$frame->size()->height()
); );
// set new content as resource // set new content as resource