-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdcd04a
commit 1c103da
Showing
3 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Intervention\Image\Drivers\Vips\Modifiers; | ||
|
||
use Intervention\Image\Colors\Rgb\Channels\Alpha; | ||
use Intervention\Image\Colors\Rgb\Channels\Blue; | ||
use Intervention\Image\Colors\Rgb\Channels\Green; | ||
use Intervention\Image\Colors\Rgb\Channels\Red; | ||
use Intervention\Image\Drivers\Vips\Core; | ||
use Intervention\Image\Exceptions\ColorException; | ||
use Intervention\Image\Interfaces\ColorInterface; | ||
use Intervention\Image\Interfaces\FrameInterface; | ||
use Intervention\Image\Interfaces\ImageInterface; | ||
use Intervention\Image\Interfaces\SizeInterface; | ||
use Jcupitt\Vips\Extend; | ||
|
||
class PadModifier extends ContainModifier | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see Intervention\Image\Interfaces\ModifierInterface::apply() | ||
*/ | ||
public function apply(ImageInterface $image): ImageInterface | ||
{ | ||
$resize = $this->getResizeSize($image); | ||
$bgColor = $this->driver()->handleInput($this->background); | ||
|
||
if (!$image->isAnimated()) { | ||
$contained = $this->pad($image->core()->first(), $resize, $bgColor)->native(); | ||
} else { | ||
$frames = []; | ||
foreach ($image as $frame) { | ||
$frames[] = $this->pad($frame, $resize, $bgColor); | ||
} | ||
|
||
$contained = Core::replaceFrames($image->core()->native(), $frames); | ||
} | ||
|
||
$image->core()->setNative($contained); | ||
|
||
return $image; | ||
} | ||
|
||
/** | ||
* Apply padded image resizing | ||
* | ||
* @param FrameInterface $frame | ||
* @param SizeInterface $resize | ||
* @param ColorInterface $bgColor | ||
* @return FrameInterface | ||
* @throws ColorException | ||
*/ | ||
private function pad(FrameInterface $frame, SizeInterface $resize, ColorInterface $bgColor): FrameInterface | ||
{ | ||
$cropWidth = min($frame->native()->width, $resize->width()); | ||
$cropHeight = min($frame->native()->height, $resize->height()); | ||
|
||
$resized = $frame->native()->thumbnail_image($cropWidth, [ | ||
'height' => $cropHeight, | ||
'no_rotate' => true, | ||
]); | ||
|
||
if (!$resized->hasAlpha()) { | ||
$resized = $resized->bandjoin_const(255); | ||
} | ||
|
||
$frame->setNative( | ||
$resized->gravity( | ||
$this->positionToGravity($this->position), | ||
$resize->width(), | ||
$resize->height(), | ||
[ | ||
'extend' => Extend::BACKGROUND, | ||
'background' => [ | ||
$bgColor->channel(Red::class)->value(), | ||
$bgColor->channel(Green::class)->value(), | ||
$bgColor->channel(Blue::class)->value(), | ||
$bgColor->channel(Alpha::class)->value(), | ||
], | ||
] | ||
) | ||
); | ||
|
||
return $frame; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Intervention\Image\Drivers\Vips\Tests\Unit\Modifiers; | ||
|
||
use Intervention\Image\Drivers\Vips\Modifiers\PadModifier; | ||
use Intervention\Image\Drivers\Vips\Tests\BaseTestCase; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
|
||
#[CoversClass(PadModifier::class)] | ||
final class PadModifierTest extends BaseTestCase | ||
{ | ||
public function testModify(): void | ||
{ | ||
$image = $this->readTestImage('blue.gif'); | ||
$this->assertEquals(16, $image->width()); | ||
$this->assertEquals(16, $image->height()); | ||
$image->modify(new PadModifier(30, 20, 'f00')); | ||
$this->assertEquals(30, $image->width()); | ||
$this->assertEquals(20, $image->height()); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(0, 0)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(0, 19)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(29, 0)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(29, 19)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(6, 2)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(7, 1)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(6, 17)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(7, 18)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(23, 1)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(23, 2)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(23, 17)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(23, 18)); | ||
$this->assertColor(100, 100, 255, 255, $image->pickColor(7, 2)); | ||
$this->assertColor(100, 100, 255, 255, $image->pickColor(22, 2)); | ||
$this->assertColor(100, 100, 255, 255, $image->pickColor(7, 17)); | ||
$this->assertColor(100, 100, 255, 255, $image->pickColor(22, 17)); | ||
} | ||
} |