mirror of
https://github.com/Intervention/image.git
synced 2025-08-29 16:50:07 +02:00
PlaceModifier
This commit is contained in:
65
src/Drivers/Gd/Modifiers/PlaceModifier.php
Normal file
65
src/Drivers/Gd/Modifiers/PlaceModifier.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Drivers\Gd\Modifiers;
|
||||
|
||||
use Intervention\Image\Drivers\Gd\Image;
|
||||
use Intervention\Image\Geometry\Point;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
use Intervention\Image\Traits\CanResolveDriverClass;
|
||||
|
||||
class PlaceModifier implements ModifierInterface
|
||||
{
|
||||
use CanResolveDriverClass;
|
||||
|
||||
protected $element;
|
||||
protected $position;
|
||||
protected $offset_x;
|
||||
protected $offset_y;
|
||||
|
||||
/**
|
||||
* Create new modifier
|
||||
*
|
||||
*/
|
||||
public function __construct($element, string $position, int $offset_x, int $offset_y)
|
||||
{
|
||||
$this->element = $element;
|
||||
$this->position = $position;
|
||||
$this->offset_x = $offset_x;
|
||||
$this->offset_y = $offset_y;
|
||||
}
|
||||
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$watermark = $this->decodeWatermark();
|
||||
$position = $this->getPosition($image, $watermark);
|
||||
|
||||
foreach ($image as $frame) {
|
||||
imagecopy(
|
||||
$frame->getCore(),
|
||||
$watermark->getFrame()->getCore(),
|
||||
$position->getX(),
|
||||
$position->getY(),
|
||||
0,
|
||||
0,
|
||||
$watermark->width(),
|
||||
$watermark->height()
|
||||
);
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
protected function decodeWatermark(): Image
|
||||
{
|
||||
return $this->resolveDriverClass('InputHandler')->handle($this->element);
|
||||
}
|
||||
|
||||
protected function getPosition(Image $image, Image $watermark): Point
|
||||
{
|
||||
$image_size = $image->getSize()->align($this->position, $this->offset_x, $this->offset_y);
|
||||
$watermark_size = $watermark->getSize()->align($this->position);
|
||||
|
||||
return $image_size->relativePosition($watermark_size);
|
||||
}
|
||||
}
|
58
src/Drivers/Imagick/Modifiers/PlaceModifier.php
Normal file
58
src/Drivers/Imagick/Modifiers/PlaceModifier.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Drivers\Imagick\Modifiers;
|
||||
|
||||
use Imagick;
|
||||
use Intervention\Image\Drivers\Imagick\Image;
|
||||
use Intervention\Image\Geometry\Point;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
use Intervention\Image\Traits\CanResolveDriverClass;
|
||||
|
||||
class PlaceModifier implements ModifierInterface
|
||||
{
|
||||
use CanResolveDriverClass;
|
||||
|
||||
protected $element;
|
||||
protected $position;
|
||||
protected $offset_x;
|
||||
protected $offset_y;
|
||||
|
||||
public function __construct($element, string $position, int $offset_x, int $offset_y)
|
||||
{
|
||||
$this->element = $element;
|
||||
$this->position = $position;
|
||||
$this->offset_x = $offset_x;
|
||||
$this->offset_y = $offset_y;
|
||||
}
|
||||
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$watermark = $this->decodeWatermark();
|
||||
$position = $this->getPosition($image, $watermark);
|
||||
|
||||
foreach ($image as $frame) {
|
||||
$frame->getCore()->compositeImage(
|
||||
$watermark->getFrame()->getCore(),
|
||||
Imagick::COMPOSITE_DEFAULT,
|
||||
$position->getX(),
|
||||
$position->getY()
|
||||
);
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
protected function decodeWatermark(): Image
|
||||
{
|
||||
return $this->resolveDriverClass('InputHandler')->handle($this->element);
|
||||
}
|
||||
|
||||
protected function getPosition(Image $image, Image $watermark): Point
|
||||
{
|
||||
$image_size = $image->getSize()->align($this->position, $this->offset_x, $this->offset_y);
|
||||
$watermark_size = $watermark->getSize()->align($this->position);
|
||||
|
||||
return $image_size->relativePosition($watermark_size);
|
||||
}
|
||||
}
|
@@ -176,4 +176,19 @@ class Size implements SizeInterface
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the relative position to another Size
|
||||
* based on the pivot point settings of both sizes.
|
||||
*
|
||||
* @param Size $size
|
||||
* @return Point
|
||||
*/
|
||||
public function relativePosition(Size $size): Point
|
||||
{
|
||||
$x = $this->getPivot()->getX() - $size->getPivot()->getX();
|
||||
$y = $this->getPivot()->getY() - $size->getPivot()->getY();
|
||||
|
||||
return new Point($x, $y);
|
||||
}
|
||||
}
|
||||
|
22
tests/Drivers/Gd/Modifiers/PlaceModifierTest.php
Normal file
22
tests/Drivers/Gd/Modifiers/PlaceModifierTest.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Tests\Drivers\Gd\Modifiers;
|
||||
|
||||
use Intervention\Image\Drivers\Gd\Image;
|
||||
use Intervention\Image\Drivers\Gd\Modifiers\PlaceModifier;
|
||||
use Intervention\Image\Geometry\Size;
|
||||
use Intervention\Image\Tests\TestCase;
|
||||
use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
|
||||
|
||||
class PlaceModifierTest extends TestCase
|
||||
{
|
||||
use CanCreateGdTestImage;
|
||||
|
||||
public function testColorChange(): void
|
||||
{
|
||||
$image = $this->createTestImage('test.jpg');
|
||||
$this->assertEquals('febc44', $image->pickColor(300, 25)->toHex());
|
||||
$image->modify(new PlaceModifier(__DIR__ . '/../../../images/circle.png', 'top-right', 0, 0));
|
||||
$this->assertEquals('32250d', $image->pickColor(300, 25)->toHex());
|
||||
}
|
||||
}
|
22
tests/Drivers/Imagick/Modifiers/PlaceModifierTest.php
Normal file
22
tests/Drivers/Imagick/Modifiers/PlaceModifierTest.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Tests\Drivers\Imagick\Modifiers;
|
||||
|
||||
use Intervention\Image\Drivers\Imagick\Image;
|
||||
use Intervention\Image\Drivers\Imagick\Modifiers\PlaceModifier;
|
||||
use Intervention\Image\Geometry\Size;
|
||||
use Intervention\Image\Tests\TestCase;
|
||||
use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
|
||||
|
||||
class PlaceModifierTest extends TestCase
|
||||
{
|
||||
use CanCreateImagickTestImage;
|
||||
|
||||
public function testColorChange(): void
|
||||
{
|
||||
$image = $this->createTestImage('test.jpg');
|
||||
$this->assertEquals('fdbd42', $image->pickColor(300, 25)->toHex());
|
||||
$image->modify(new PlaceModifier(__DIR__ . '/../../../images/circle.png', 'top-right', 0, 0));
|
||||
$this->assertEquals('33260d', $image->pickColor(300, 25)->toHex());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user