1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 20:28:21 +01:00

Modifiers

This commit is contained in:
Oliver Vogel 2021-11-09 14:59:59 +00:00
parent e168824057
commit 84e946a588
6 changed files with 65 additions and 43 deletions

View File

@ -0,0 +1,30 @@
<?php
namespace Intervention\Image\Drivers\Abstract\Modifiers;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Traits\CanResizeGeometrically;
abstract class AbstractResizeModifier implements ModifierInterface
{
use CanResizeGeometrically;
protected $target;
public function __construct(SizeInterface $target)
{
$this->target = $target;
}
protected function getCropSize(ImageInterface $image): SizeInterface
{
return $image->getSize();
}
protected function getResizeSize(ImageInterface $image): SizeInterface
{
return $this->target;
}
}

View File

@ -2,13 +2,9 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Geometry\Resizer;
use Intervention\Image\Geometry\Size;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Traits\CanResizeGeometrically;
class FitDownModifier extends FitModifier implements ModifierInterface
{

View File

@ -2,13 +2,9 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Geometry\Resizer;
use Intervention\Image\Geometry\Size;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Traits\CanResizeGeometrically;
class FitModifier extends ResizeModifier implements ModifierInterface
{

View File

@ -2,23 +2,15 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractResizeModifier;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Traits\CanResizeGeometrically;
class ResizeModifier implements ModifierInterface
class ResizeModifier extends AbstractResizeModifier implements ModifierInterface
{
use CanResizeGeometrically;
protected $target;
public function __construct(SizeInterface $target)
{
$this->target = $target;
}
public function apply(ImageInterface $image): ImageInterface
{
$crop = $this->getCropSize($image);
@ -31,16 +23,6 @@ class ResizeModifier implements ModifierInterface
return $image;
}
protected function getCropSize(ImageInterface $image): SizeInterface
{
return $image->getSize();
}
protected function getResizeSize(ImageInterface $image): SizeInterface
{
return $this->target;
}
protected function modify(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void
{
// create new image

View File

@ -2,31 +2,32 @@
namespace Intervention\Image\Drivers\Imagick\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractResizeModifier;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SizeInterface;
class ResizeModifier implements ModifierInterface
class ResizeModifier extends AbstractResizeModifier implements ModifierInterface
{
/**
* Target size
*
* @var SizeInterface
*/
protected $target;
public function __construct(SizeInterface $target)
{
$this->target = $target;
}
public function apply(ImageInterface $image): ImageInterface
{
$resize = $this->getResizeSize($image);
$crop = $this->getResizeSize($image);
$shouldCrop = $crop != $image->getSize();
foreach ($image as $frame) {
if ($shouldCrop) {
$frame->getCore()->cropImage(
$crop->getWidth(),
$crop->getHeight(),
$crop->getPivot()->getX(),
$crop->getPivot()->getY()
);
}
$frame->getCore()->scaleImage(
$this->target->getWidth(),
$this->target->getHeight()
$resize->getWidth(),
$resize->getHeight()
);
}

View File

@ -20,6 +20,23 @@ class SizeTest extends TestCase
$this->assertEquals(200, $size->getHeight());
}
public function testCompareSizes(): void
{
$size1 = new Size(300, 200);
$size2 = new Size(300, 200);
$size2a = new Size(300, 200, new Point(1, 1));
$size2b = new Size(300, 200, new Point(1, 1));
$size3 = new Size(300, 201);
$size4 = new Size(301, 200);
$this->assertTrue($size1 == $size2);
$this->assertTrue($size2a == $size2b);
$this->assertFalse($size2 == $size2a);
$this->assertFalse($size2 == $size3);
$this->assertFalse($size2 == $size4);
$this->assertFalse($size3 == $size4);
}
public function testGetWidth()
{
$size = new Size(800, 600);