1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-29 16:50:07 +02:00

PlaceModifier

This commit is contained in:
Oliver Vogel
2021-11-05 17:13:27 +00:00
parent 1b856c81bc
commit 7c6c7be2ba
5 changed files with 182 additions and 0 deletions

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

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

View File

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

View 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());
}
}

View 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());
}
}