mirror of
https://github.com/Intervention/image.git
synced 2025-09-03 02:42:45 +02:00
Add ResizeCanvasRelativeModifier
This commit is contained in:
14
src/Drivers/Gd/Modifiers/ResizeCanvasRelativeModifier.php
Normal file
14
src/Drivers/Gd/Modifiers/ResizeCanvasRelativeModifier.php
Normal 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
|
||||||
|
{
|
||||||
|
}
|
@@ -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
|
||||||
|
{
|
||||||
|
}
|
@@ -65,6 +65,7 @@ use Intervention\Image\Modifiers\ProfileModifier;
|
|||||||
use Intervention\Image\Modifiers\ProfileRemovalModifier;
|
use Intervention\Image\Modifiers\ProfileRemovalModifier;
|
||||||
use Intervention\Image\Modifiers\RemoveAnimationModifier;
|
use Intervention\Image\Modifiers\RemoveAnimationModifier;
|
||||||
use Intervention\Image\Modifiers\ResizeCanvasModifier;
|
use Intervention\Image\Modifiers\ResizeCanvasModifier;
|
||||||
|
use Intervention\Image\Modifiers\ResizeCanvasRelativeModifier;
|
||||||
use Intervention\Image\Modifiers\ResizeDownModifier;
|
use Intervention\Image\Modifiers\ResizeDownModifier;
|
||||||
use Intervention\Image\Modifiers\ResizeModifier;
|
use Intervention\Image\Modifiers\ResizeModifier;
|
||||||
use Intervention\Image\Modifiers\ResolutionModifier;
|
use Intervention\Image\Modifiers\ResolutionModifier;
|
||||||
@@ -528,14 +529,23 @@ final class Image implements ImageInterface, Countable
|
|||||||
* @see ImageInterface::resizeCanvas()
|
* @see ImageInterface::resizeCanvas()
|
||||||
*/
|
*/
|
||||||
public function resizeCanvas(
|
public function resizeCanvas(
|
||||||
int $width,
|
?int $width = null,
|
||||||
int $height,
|
?int $height = null,
|
||||||
mixed $background = 'ffffff',
|
mixed $background = 'ffffff',
|
||||||
string $position = 'center'
|
string $position = 'center'
|
||||||
): ImageInterface {
|
): ImageInterface {
|
||||||
return $this->modify(new ResizeCanvasModifier($width, $height, $background, $position));
|
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}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
|
@@ -353,15 +353,33 @@ interface ImageInterface extends IteratorAggregate, Countable
|
|||||||
* image the resizing is going to happen. A background color can be passed
|
* image the resizing is going to happen. A background color can be passed
|
||||||
* to define the color of the new emerging areas.
|
* to define the color of the new emerging areas.
|
||||||
*
|
*
|
||||||
* @param int $width
|
* @param null|int $width
|
||||||
* @param int $height
|
* @param null|int $height
|
||||||
* @param string $position
|
* @param string $position
|
||||||
* @param mixed $background
|
* @param mixed $background
|
||||||
* @return ImageInterface
|
* @return ImageInterface
|
||||||
*/
|
*/
|
||||||
public function resizeCanvas(
|
public function resizeCanvas(
|
||||||
int $width,
|
?int $width = null,
|
||||||
int $height,
|
?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',
|
mixed $background = 'ffffff',
|
||||||
string $position = 'center'
|
string $position = 'center'
|
||||||
): ImageInterface;
|
): ImageInterface;
|
||||||
|
@@ -9,8 +9,8 @@ use Intervention\Image\Interfaces\SizeInterface;
|
|||||||
class ResizeCanvasModifier extends AbstractModifier
|
class ResizeCanvasModifier extends AbstractModifier
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public int $width,
|
public ?int $width = null,
|
||||||
public int $height,
|
public ?int $height = null,
|
||||||
public mixed $background = 'ffffff',
|
public mixed $background = 'ffffff',
|
||||||
public string $position = 'center'
|
public string $position = 'center'
|
||||||
) {
|
) {
|
||||||
@@ -18,7 +18,10 @@ class ResizeCanvasModifier extends AbstractModifier
|
|||||||
|
|
||||||
public function cropSize(ImageInterface $image): SizeInterface
|
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);
|
->alignPivotTo($image->size(), $this->position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
src/Modifiers/ResizeCanvasRelativeModifier.php
Normal file
19
src/Modifiers/ResizeCanvasRelativeModifier.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user