1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 17:41:58 +02:00

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()
This commit is contained in:
Oliver Vogel
2025-06-21 09:25:17 +02:00
parent a20c9833da
commit 2e499225bf
17 changed files with 110 additions and 44 deletions

View File

@@ -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()

View File

@@ -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

View File

@@ -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);

View File

@@ -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);

View File

@@ -24,7 +24,7 @@ class ResizeCanvasModifier extends GenericResizeCanvasModifier implements Specia
$cropSize->height(),
$cropSize->pivot()->x(),
$cropSize->pivot()->y(),
$this->background,
$this->backgroundColor(),
));
return $image;

View File

@@ -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);

View File

@@ -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(

View File

@@ -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

View File

@@ -19,7 +19,7 @@ class ResizeCanvasModifier extends GenericResizeCanvasModifier implements Specia
$cropSize->height(),
$cropSize->pivot()->x(),
$cropSize->pivot()->y(),
$this->background,
$this->backgroundColor(),
));
return $image;

View File

@@ -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(

View File

@@ -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));

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}