1
0
mirror of https://github.com/Intervention/image.git synced 2025-03-15 22:49:40 +01:00

Add ResizeCanvasRelativeModifier

This commit is contained in:
Oliver Vogel 2023-12-06 16:33:43 +01:00
parent 1e0de6501a
commit 87b90afcb2
8 changed files with 149 additions and 9 deletions

View File

@ -0,0 +1,14 @@
<?php
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
/**
* @method SizeInterface cropSize(ImageInterface $image)
* @property mixed $background
*/
class ResizeCanvasRelativeModifier extends ResizeCanvasModifier
{
}

View File

@ -0,0 +1,14 @@
<?php
namespace Intervention\Image\Drivers\Imagick\Modifiers;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
/**
* @method SizeInterface cropSize(ImageInterface $image)
* @property mixed $background
*/
class ResizeCanvasRelativeModifier extends ResizeCanvasModifier
{
}

View File

@ -65,6 +65,7 @@ use Intervention\Image\Modifiers\ProfileModifier;
use Intervention\Image\Modifiers\ProfileRemovalModifier;
use Intervention\Image\Modifiers\RemoveAnimationModifier;
use Intervention\Image\Modifiers\ResizeCanvasModifier;
use Intervention\Image\Modifiers\ResizeCanvasRelativeModifier;
use Intervention\Image\Modifiers\ResizeDownModifier;
use Intervention\Image\Modifiers\ResizeModifier;
use Intervention\Image\Modifiers\ResolutionModifier;
@ -528,14 +529,23 @@ final class Image implements ImageInterface, Countable
* @see ImageInterface::resizeCanvas()
*/
public function resizeCanvas(
int $width,
int $height,
?int $width = null,
?int $height = null,
mixed $background = 'ffffff',
string $position = 'center'
): ImageInterface {
return $this->modify(new ResizeCanvasModifier($width, $height, $background, $position));
}
public function resizeCanvasRelative(
?int $width = null,
?int $height = null,
mixed $background = 'ffffff',
string $position = 'center',
): ImageInterface {
return $this->modify(new ResizeCanvasRelativeModifier($width, $height, $background, $position));
}
/**
* {@inheritdoc}
*

View File

@ -353,15 +353,33 @@ interface ImageInterface extends IteratorAggregate, Countable
* image the resizing is going to happen. A background color can be passed
* to define the color of the new emerging areas.
*
* @param int $width
* @param int $height
* @param null|int $width
* @param null|int $height
* @param string $position
* @param mixed $background
* @return ImageInterface
*/
public function resizeCanvas(
int $width,
int $height,
?int $width = null,
?int $height = null,
mixed $background = 'ffffff',
string $position = 'center'
): ImageInterface;
/**
* Resize canvas in the same way as resizeCanvas() but takes relative values
* for the width and height, which will be added or subtracted to the
* original image size.
*
* @param null|int $width
* @param null|int $height
* @param string $position
* @param mixed $background
* @return ImageInterface
*/
public function resizeCanvasRelative(
?int $width = null,
?int $height = null,
mixed $background = 'ffffff',
string $position = 'center'
): ImageInterface;

View File

@ -9,8 +9,8 @@ use Intervention\Image\Interfaces\SizeInterface;
class ResizeCanvasModifier extends AbstractModifier
{
public function __construct(
public int $width,
public int $height,
public ?int $width = null,
public ?int $height = null,
public mixed $background = 'ffffff',
public string $position = 'center'
) {
@ -18,7 +18,10 @@ class ResizeCanvasModifier extends AbstractModifier
public function cropSize(ImageInterface $image): SizeInterface
{
return (new Rectangle($this->width, $this->height))
$width = is_null($this->width) ? $image->width() : $this->width;
$height = is_null($this->height) ? $image->height() : $this->height;
return (new Rectangle($width, $height))
->alignPivotTo($image->size(), $this->position);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Intervention\Image\Modifiers;
use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
class ResizeCanvasRelativeModifier extends ResizeCanvasModifier
{
public function cropSize(ImageInterface $image): SizeInterface
{
$width = is_null($this->width) ? $image->width() : $image->width() + $this->width;
$height = is_null($this->height) ? $image->height() : $image->height() + $this->height;
return (new Rectangle($width, $height))
->alignPivotTo($image->size(), $this->position);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Intervention\Image\Tests\Drivers\Gd\Modifiers;
use Intervention\Image\Modifiers\ResizeCanvasRelativeModifier;
use Intervention\Image\Tests\TestCase;
use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
/**
* @requires extension gd
* @covers \Intervention\Image\Modifiers\ResizeCanvasRelativeModifier
*/
class ResizeCanvasRelativeModifierTest extends TestCase
{
use CanCreateGdTestImage;
public function testModify(): void
{
$image = $this->createTestImage('tile.png');
$this->assertEquals(16, $image->width());
$this->assertEquals(16, $image->height());
$image->modify(new ResizeCanvasRelativeModifier(2, 2, 'ff0', 'center'));
$this->assertEquals(18, $image->width());
$this->assertEquals(18, $image->height());
$this->assertColor(255, 255, 0, 255, $image->pickColor(0, 0));
$this->assertColor(180, 224, 0, 255, $image->pickColor(1, 1));
$this->assertColor(180, 224, 0, 255, $image->pickColor(2, 2));
$this->assertColor(255, 255, 0, 255, $image->pickColor(17, 17));
$this->assertTransparency($image->pickColor(12, 1));
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick\Modifiers;
use Intervention\Image\Modifiers\ResizeCanvasRelativeModifier;
use Intervention\Image\Tests\TestCase;
use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
/**
* @requires extension gd
* @covers \Intervention\Image\Modifiers\ResizeCanvasRelativeModifier
*/
class ResizeCanvasRelativeModifierTest extends TestCase
{
use CanCreateImagickTestImage;
public function testModify(): void
{
$image = $this->createTestImage('tile.png');
$this->assertEquals(16, $image->width());
$this->assertEquals(16, $image->height());
$image->modify(new ResizeCanvasRelativeModifier(2, 2, 'ff0', 'center'));
$this->assertEquals(18, $image->width());
$this->assertEquals(18, $image->height());
$this->assertColor(255, 255, 0, 255, $image->pickColor(0, 0));
$this->assertColor(180, 224, 0, 255, $image->pickColor(1, 1));
$this->assertColor(180, 224, 0, 255, $image->pickColor(2, 2));
$this->assertColor(255, 255, 0, 255, $image->pickColor(17, 17));
$this->assertTransparency($image->pickColor(12, 1));
}
}