1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 01:29:51 +02:00

CropResizeModifier

This commit is contained in:
Oliver Vogel
2021-11-11 14:25:23 +00:00
parent ba7fc0e42e
commit 989166aced
5 changed files with 45 additions and 124 deletions

View File

@@ -1,30 +0,0 @@
<?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

@@ -1,18 +0,0 @@
<?php
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SizeInterface;
class FitDownModifier extends FitModifier implements ModifierInterface
{
protected function getResizeSize(ImageInterface $image): SizeInterface
{
return $this->resizeGeometrically($this->getCropSize($image))
->toWidth($this->target->getWidth())
->toHeight($this->target->getHeight())
->scaleDown();
}
}

View File

@@ -1,40 +0,0 @@
<?php
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SizeInterface;
class FitModifier extends CropResizeModifier implements ModifierInterface
{
protected function getCropSize(ImageInterface $image): SizeInterface
{
$imagesize = $image->getSize();
// auto height
$size = $this->resizeGeometrically($this->target)
->toWidth($imagesize->getWidth())
->scale();
if (!$size->fitsInto($imagesize)) {
// auto width
$size = $this->resizeGeometrically($this->target)
->toHeight($imagesize->getHeight())
->scale();
}
return $size->alignPivotTo(
$imagesize->alignPivot($this->position),
$this->position
);
}
protected function getResizeSize(ImageInterface $image): SizeInterface
{
return $this->resizeGeometrically($this->getCropSize($image))
->toWidth($this->target->getWidth())
->toHeight($this->target->getHeight())
->scale();
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Intervention\Image\Drivers\Imagick\Modifiers;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SizeInterface;
class CropResizeModifier implements ModifierInterface
{
protected $crop;
protected $resize;
protected $position;
public function __construct(SizeInterface $crop, SizeInterface $resize, string $position = 'top-left')
{
$this->crop = $crop;
$this->resize = $resize;
$this->position = $position;
}
public function apply(ImageInterface $image): ImageInterface
{
$shouldCrop = $this->crop != $image->getSize();
foreach ($image as $frame) {
if ($shouldCrop) {
$frame->getCore()->cropImage(
$this->crop->getWidth(),
$this->crop->getHeight(),
$this->crop->getPivot()->getX(),
$this->crop->getPivot()->getY()
);
}
$frame->getCore()->scaleImage(
$this->resize->getWidth(),
$this->resize->getHeight()
);
}
return $image;
}
}

View File

@@ -1,36 +0,0 @@
<?php
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 extends AbstractResizeModifier implements ModifierInterface
{
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(
$resize->getWidth(),
$resize->getHeight()
);
}
return $image;
}
}