From 2e499225bff6c693269297fad3c116283ebae775 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 21 Jun 2025 09:25:17 +0200 Subject: [PATCH] Change default value of background argument The following mehthods will fallback to the currently configured background color instead of usisng the hard coded value of `ffffff`: - ImageInterface::rotate() - ImageInterface::resizeCanvas() - ImageInterface::resizeCanvasRelative() - ImageInterface::contain() - ImageInterface::pad() - ImageInterface::crop() --- changelog.md | 6 ++++++ src/Drivers/Gd/Modifiers/ContainModifier.php | 10 +++------- src/Drivers/Gd/Modifiers/CropModifier.php | 2 +- .../Gd/Modifiers/QuantizeColorsModifier.php | 17 ++++++++--------- .../Gd/Modifiers/ResizeCanvasModifier.php | 2 +- src/Drivers/Gd/Modifiers/RotateModifier.php | 2 +- .../Imagick/Modifiers/ContainModifier.php | 9 ++++++--- src/Drivers/Imagick/Modifiers/CropModifier.php | 2 +- .../Imagick/Modifiers/ResizeCanvasModifier.php | 2 +- .../Imagick/Modifiers/RotateModifier.php | 8 +++++--- src/Image.php | 12 ++++++------ src/Interfaces/ImageInterface.php | 12 ++++++------ src/Modifiers/ContainModifier.php | 16 +++++++++++++++- src/Modifiers/CropModifier.php | 13 ++++++++++++- src/Modifiers/QuantizeColorsModifier.php | 14 +++++++++++++- src/Modifiers/ResizeCanvasModifier.php | 13 ++++++++++++- src/Modifiers/RotateModifier.php | 14 +++++++++++++- 17 files changed, 110 insertions(+), 44 deletions(-) diff --git a/changelog.md b/changelog.md index b7a7c7a8..6f91805c 100644 --- a/changelog.md +++ b/changelog.md @@ -7,3 +7,9 @@ - ImageInterface::blendTransparency() was renamed to ImageInterface::background() - ImageInterface::setBlendingColor() was renamed to ImageInterface::setBackgroundColor() - ImageInterface::blendingColor() was renamed to ImageInterface::backgroundColor() +- Changed default value for `background` to `null` in ImageInterface::rotate() +- Changed default value for `background` to `null` in ImageInterface::resizeCanvas() +- Changed default value for `background` to `null` in ImageInterface::resizeCanvasRelative() +- Changed default value for `background` to `null` in ImageInterface::contain() +- Changed default value for `background` to `null` in ImageInterface::pad() +- Changed default value for `background` to `null` in ImageInterface::crop() diff --git a/src/Drivers/Gd/Modifiers/ContainModifier.php b/src/Drivers/Gd/Modifiers/ContainModifier.php index 37530673..e79f8710 100644 --- a/src/Drivers/Gd/Modifiers/ContainModifier.php +++ b/src/Drivers/Gd/Modifiers/ContainModifier.php @@ -27,13 +27,10 @@ class ContainModifier extends GenericContainModifier implements SpecializedInter { $crop = $this->getCropSize($image); $resize = $this->getResizeSize($image); - $background = $this->driver()->handleInput($this->background); - $backgroundColor = $this->driver()->handleInput( - $this->driver()->config()->backgroundColor - ); + $backgroundColor = $this->backgroundColor(); foreach ($image as $frame) { - $this->modify($frame, $crop, $resize, $background, $backgroundColor); + $this->modify($frame, $crop, $resize, $backgroundColor); } return $image; @@ -46,11 +43,10 @@ class ContainModifier extends GenericContainModifier implements SpecializedInter FrameInterface $frame, SizeInterface $crop, SizeInterface $resize, - ColorInterface $background, ColorInterface $backgroundColor ): void { // create new gd image - $modified = Cloner::cloneEmpty($frame->native(), $resize, $background); + $modified = Cloner::cloneEmpty($frame->native(), $resize, $backgroundColor); // make image area transparent to keep transparency // even if background-color is set diff --git a/src/Drivers/Gd/Modifiers/CropModifier.php b/src/Drivers/Gd/Modifiers/CropModifier.php index c1ea4825..96a53233 100644 --- a/src/Drivers/Gd/Modifiers/CropModifier.php +++ b/src/Drivers/Gd/Modifiers/CropModifier.php @@ -24,7 +24,7 @@ class CropModifier extends GenericCropModifier implements SpecializedInterface { $originalSize = $image->size(); $crop = $this->crop($image); - $background = $this->driver()->handleInput($this->background); + $background = $this->backgroundColor(); foreach ($image as $frame) { $this->cropFrame($frame, $originalSize, $crop, $background); diff --git a/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php b/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php index 9d5d6f53..a04af703 100644 --- a/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php +++ b/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php @@ -32,23 +32,22 @@ class QuantizeColorsModifier extends GenericQuantizeColorsModifier implements Sp $width = $image->width(); $height = $image->height(); - $background = $this->driver()->colorProcessor($image->colorspace())->colorToNative( - $this->driver()->handleInput($this->background) - ); - - $backgroundColor = $this->driver()->handleInput( - $this->driver()->config()->backgroundColor - ); + $backgroundColor = $this->backgroundColor(); + $nativeBackgroundColor = $this->driver() + ->colorProcessor($image->colorspace()) + ->colorToNative( + $backgroundColor + ); foreach ($image as $frame) { // create new image for color quantization $reduced = Cloner::cloneEmpty($frame->native(), background: $backgroundColor); // fill with background - imagefill($reduced, 0, 0, $background); + imagefill($reduced, 0, 0, $nativeBackgroundColor); // set transparency - imagecolortransparent($reduced, $background); + imagecolortransparent($reduced, $nativeBackgroundColor); // copy original image (colors are limited automatically in the copy process) imagecopy($reduced, $frame->native(), 0, 0, 0, 0, $width, $height); diff --git a/src/Drivers/Gd/Modifiers/ResizeCanvasModifier.php b/src/Drivers/Gd/Modifiers/ResizeCanvasModifier.php index 7f73cc73..5f655b20 100644 --- a/src/Drivers/Gd/Modifiers/ResizeCanvasModifier.php +++ b/src/Drivers/Gd/Modifiers/ResizeCanvasModifier.php @@ -24,7 +24,7 @@ class ResizeCanvasModifier extends GenericResizeCanvasModifier implements Specia $cropSize->height(), $cropSize->pivot()->x(), $cropSize->pivot()->y(), - $this->background, + $this->backgroundColor(), )); return $image; diff --git a/src/Drivers/Gd/Modifiers/RotateModifier.php b/src/Drivers/Gd/Modifiers/RotateModifier.php index 4db07502..d32d5a52 100644 --- a/src/Drivers/Gd/Modifiers/RotateModifier.php +++ b/src/Drivers/Gd/Modifiers/RotateModifier.php @@ -25,7 +25,7 @@ class RotateModifier extends GenericRotateModifier implements SpecializedInterfa */ public function apply(ImageInterface $image): ImageInterface { - $background = $this->driver()->handleInput($this->background); + $background = $this->backgroundColor(); foreach ($image as $frame) { $this->modifyFrame($frame, $background); diff --git a/src/Drivers/Imagick/Modifiers/ContainModifier.php b/src/Drivers/Imagick/Modifiers/ContainModifier.php index b01bfd3b..77f83e62 100644 --- a/src/Drivers/Imagick/Modifiers/ContainModifier.php +++ b/src/Drivers/Imagick/Modifiers/ContainModifier.php @@ -17,9 +17,12 @@ class ContainModifier extends GenericContainModifier implements SpecializedInter $crop = $this->getCropSize($image); $resize = $this->getResizeSize($image); $transparent = new ImagickPixel('transparent'); - $background = $this->driver()->colorProcessor($image->colorspace())->colorToNative( - $this->driver()->handleInput($this->background) - ); + + $background = $this->driver() + ->colorProcessor($image->colorspace()) + ->colorToNative( + $this->backgroundColor() + ); foreach ($image as $frame) { $frame->native()->scaleImage( diff --git a/src/Drivers/Imagick/Modifiers/CropModifier.php b/src/Drivers/Imagick/Modifiers/CropModifier.php index 6a2e8d31..96c2d72b 100644 --- a/src/Drivers/Imagick/Modifiers/CropModifier.php +++ b/src/Drivers/Imagick/Modifiers/CropModifier.php @@ -16,7 +16,7 @@ class CropModifier extends GenericCropModifier implements SpecializedInterface { // decode background color $background = $this->driver()->colorProcessor($image->colorspace())->colorToNative( - $this->driver()->handleInput($this->background) + $this->backgroundColor() ); // create empty container imagick to rebuild core diff --git a/src/Drivers/Imagick/Modifiers/ResizeCanvasModifier.php b/src/Drivers/Imagick/Modifiers/ResizeCanvasModifier.php index 4459f329..0d562ebc 100644 --- a/src/Drivers/Imagick/Modifiers/ResizeCanvasModifier.php +++ b/src/Drivers/Imagick/Modifiers/ResizeCanvasModifier.php @@ -19,7 +19,7 @@ class ResizeCanvasModifier extends GenericResizeCanvasModifier implements Specia $cropSize->height(), $cropSize->pivot()->x(), $cropSize->pivot()->y(), - $this->background, + $this->backgroundColor(), )); return $image; diff --git a/src/Drivers/Imagick/Modifiers/RotateModifier.php b/src/Drivers/Imagick/Modifiers/RotateModifier.php index 19ad2282..5fc194fa 100644 --- a/src/Drivers/Imagick/Modifiers/RotateModifier.php +++ b/src/Drivers/Imagick/Modifiers/RotateModifier.php @@ -12,9 +12,11 @@ class RotateModifier extends GenericRotateModifier implements SpecializedInterfa { public function apply(ImageInterface $image): ImageInterface { - $background = $this->driver()->colorProcessor($image->colorspace())->colorToNative( - $this->driver()->handleInput($this->background) - ); + $background = $this->driver() + ->colorProcessor($image->colorspace()) + ->colorToNative( + $this->backgroundColor() + ); foreach ($image as $frame) { $frame->native()->rotateImage( diff --git a/src/Image.php b/src/Image.php index 28b09405..70264ce3 100644 --- a/src/Image.php +++ b/src/Image.php @@ -594,7 +594,7 @@ final class Image implements ImageInterface * * @see ImageInterface::rotate() */ - public function rotate(float $angle, mixed $background = 'ffffff'): ImageInterface + public function rotate(float $angle, mixed $background = null): ImageInterface { return $this->modify(new RotateModifier($angle, $background)); } @@ -693,7 +693,7 @@ final class Image implements ImageInterface public function resizeCanvas( ?int $width = null, ?int $height = null, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'center' ): ImageInterface { return $this->modify(new ResizeCanvasModifier($width, $height, $background, $position)); @@ -707,7 +707,7 @@ final class Image implements ImageInterface public function resizeCanvasRelative( ?int $width = null, ?int $height = null, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'center' ): ImageInterface { return $this->modify(new ResizeCanvasRelativeModifier($width, $height, $background, $position)); @@ -721,7 +721,7 @@ final class Image implements ImageInterface public function pad( int $width, int $height, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'center' ): ImageInterface { return $this->modify(new PadModifier($width, $height, $background, $position)); @@ -735,7 +735,7 @@ final class Image implements ImageInterface public function contain( int $width, int $height, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'center' ): ImageInterface { return $this->modify(new ContainModifier($width, $height, $background, $position)); @@ -751,7 +751,7 @@ final class Image implements ImageInterface int $height, int $offset_x = 0, int $offset_y = 0, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'top-left' ): ImageInterface { return $this->modify(new CropModifier($width, $height, $offset_x, $offset_y, $background, $position)); diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index 35a7ba88..c0f114fb 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -386,7 +386,7 @@ interface ImageInterface extends IteratorAggregate, Countable * @param string $background * @throws RuntimeException */ - public function rotate(float $angle, mixed $background = 'ffffff'): self; + public function rotate(float $angle, mixed $background = null): self; /** * Rotate the image to be upright according to exif information @@ -477,7 +477,7 @@ interface ImageInterface extends IteratorAggregate, Countable public function resizeCanvas( ?int $width = null, ?int $height = null, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'center' ): self; @@ -493,7 +493,7 @@ interface ImageInterface extends IteratorAggregate, Countable public function resizeCanvasRelative( ?int $width = null, ?int $height = null, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'center' ): self; @@ -514,7 +514,7 @@ interface ImageInterface extends IteratorAggregate, Countable public function pad( int $width, int $height, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'center' ): self; @@ -530,7 +530,7 @@ interface ImageInterface extends IteratorAggregate, Countable public function contain( int $width, int $height, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'center' ): self; @@ -548,7 +548,7 @@ interface ImageInterface extends IteratorAggregate, Countable int $height, int $offset_x = 0, int $offset_y = 0, - mixed $background = 'ffffff', + mixed $background = null, string $position = 'top-left' ): self; diff --git a/src/Modifiers/ContainModifier.php b/src/Modifiers/ContainModifier.php index 90da85d8..30b97da6 100644 --- a/src/Modifiers/ContainModifier.php +++ b/src/Modifiers/ContainModifier.php @@ -7,6 +7,7 @@ namespace Intervention\Image\Modifiers; use Intervention\Image\Drivers\SpecializableModifier; use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Geometry\Rectangle; +use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SizeInterface; @@ -15,7 +16,7 @@ class ContainModifier extends SpecializableModifier public function __construct( public int $width, public int $height, - public mixed $background = 'ffffff', + public mixed $background = null, public string $position = 'center' ) { // @@ -37,8 +38,21 @@ class ContainModifier extends SpecializableModifier ); } + /** + * Return target size for resizing + */ public function getResizeSize(ImageInterface $image): SizeInterface { return new Rectangle($this->width, $this->height); } + + /** + * Return color to fill the newly created areas after rotation + * + * @throws RuntimeException + */ + protected function backgroundColor(): ColorInterface + { + return $this->driver()->handleInput($this->background ?? $this->driver()->config()->backgroundColor); + } } diff --git a/src/Modifiers/CropModifier.php b/src/Modifiers/CropModifier.php index c9aa5855..4c7aa435 100644 --- a/src/Modifiers/CropModifier.php +++ b/src/Modifiers/CropModifier.php @@ -7,6 +7,7 @@ namespace Intervention\Image\Modifiers; use Intervention\Image\Drivers\SpecializableModifier; use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Geometry\Rectangle; +use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SizeInterface; @@ -22,7 +23,7 @@ class CropModifier extends SpecializableModifier public int $height, public int $offset_x = 0, public int $offset_y = 0, - public mixed $background = 'ffffff', + public mixed $background = null, public string $position = 'top-left' ) { // @@ -41,4 +42,14 @@ class CropModifier extends SpecializableModifier $this->position ); } + + /** + * Return color to fill the newly created areas after rotation + * + * @throws RuntimeException + */ + protected function backgroundColor(): ColorInterface + { + return $this->driver()->handleInput($this->background ?? $this->driver()->config()->backgroundColor); + } } diff --git a/src/Modifiers/QuantizeColorsModifier.php b/src/Modifiers/QuantizeColorsModifier.php index 193c12b1..2c5b49eb 100644 --- a/src/Modifiers/QuantizeColorsModifier.php +++ b/src/Modifiers/QuantizeColorsModifier.php @@ -5,6 +5,8 @@ declare(strict_types=1); namespace Intervention\Image\Modifiers; use Intervention\Image\Drivers\SpecializableModifier; +use Intervention\Image\Interfaces\ColorInterface; +use Intervention\Image\Exceptions\RuntimeException; class QuantizeColorsModifier extends SpecializableModifier { @@ -15,8 +17,18 @@ class QuantizeColorsModifier extends SpecializableModifier */ public function __construct( public int $limit, - public mixed $background = 'ffffff' + public mixed $background = 'transparent' ) { // } + + /** + * Return color to fill the newly created areas after rotation + * + * @throws RuntimeException + */ + protected function backgroundColor(): ColorInterface + { + return $this->driver()->handleInput($this->background ?? $this->driver()->config()->backgroundColor); + } } diff --git a/src/Modifiers/ResizeCanvasModifier.php b/src/Modifiers/ResizeCanvasModifier.php index ae42b7a5..5dbcc5f3 100644 --- a/src/Modifiers/ResizeCanvasModifier.php +++ b/src/Modifiers/ResizeCanvasModifier.php @@ -7,6 +7,7 @@ namespace Intervention\Image\Modifiers; use Intervention\Image\Drivers\SpecializableModifier; use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Geometry\Rectangle; +use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SizeInterface; @@ -20,7 +21,7 @@ class ResizeCanvasModifier extends SpecializableModifier public function __construct( public ?int $width = null, public ?int $height = null, - public mixed $background = 'ffffff', + public mixed $background = null, public string $position = 'center' ) { // @@ -46,4 +47,14 @@ class ResizeCanvasModifier extends SpecializableModifier return $size->alignPivotTo($image->size(), $this->position); } + + /** + * Return color to fill the newly created areas after rotation + * + * @throws RuntimeException + */ + protected function backgroundColor(): ColorInterface + { + return $this->driver()->handleInput($this->background ?? $this->driver()->config()->backgroundColor); + } } diff --git a/src/Modifiers/RotateModifier.php b/src/Modifiers/RotateModifier.php index 15b758d9..775c0eda 100644 --- a/src/Modifiers/RotateModifier.php +++ b/src/Modifiers/RotateModifier.php @@ -5,10 +5,12 @@ declare(strict_types=1); namespace Intervention\Image\Modifiers; use Intervention\Image\Drivers\SpecializableModifier; +use Intervention\Image\Exceptions\RuntimeException; +use Intervention\Image\Interfaces\ColorInterface; class RotateModifier extends SpecializableModifier { - public function __construct(public float $angle, public mixed $background) + public function __construct(public float $angle, public mixed $background = null) { // } @@ -21,4 +23,14 @@ class RotateModifier extends SpecializableModifier { return fmod($this->angle, 360); } + + /** + * Return color to fill the newly created areas after rotation + * + * @throws RuntimeException + */ + protected function backgroundColor(): ColorInterface + { + return $this->driver()->handleInput($this->background ?? $this->driver()->config()->backgroundColor); + } }