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

FitModifier

This commit is contained in:
Oliver Vogel
2021-11-08 19:52:45 +01:00
parent b00ae906cb
commit 585766b4af
4 changed files with 44 additions and 16 deletions

View File

@@ -175,6 +175,15 @@ abstract class AbstractImage
);
}
public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface
{
$size = new Size($width, $height);
return $this->modify(
$this->resolveDriverClass('Modifiers\FitDownModifier', $size, $position)
);
}
public function place($element, string $position = 'top-left', int $offset_x = 0, int $offset_y = 0): ImageInterface
{
return $this->modify(

View File

@@ -0,0 +1,22 @@
<?php
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
{
protected function getResizeSize(ImageInterface $image): SizeInterface
{
return $this->resizeGeometrically($this->getCropSize($image))
->toWidth($this->target->getWidth())
->toHeight($this->target->getHeight())
->scaleDown();
}
}

View File

@@ -10,18 +10,6 @@ use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Traits\CanResizeGeometrically;
/*
# contain
1. Scale (keep aspect ratio) Original to fit Target
2. Scale (keep aspect ratio) Up/Down to fit Target (obsolete)
# cover
1. Scale (keep aspect ratio) Target to fit Original
2. Scale (keep aspect ratio) Up/Down to fit Target
*/
class FitModifier extends ResizeModifier implements ModifierInterface
{
use CanResizeGeometrically;
@@ -49,13 +37,22 @@ class FitModifier extends ResizeModifier implements ModifierInterface
protected function getCropSize(ImageInterface $image): SizeInterface
{
$imagesize = $image->getSize();
// auto height
$size = $this->resizeGeometrically($this->target)
->toWidth($image->width())
->toHeight($image->height())
->toWidth($imagesize->getWidth())
->scale();
if (!$size->fitsInto($imagesize)) {
// auto width
$size = $this->resizeGeometrically($this->target)
->toHeight($imagesize->getHeight())
->scale();
}
return $size->alignPivotTo(
$image->getSize()->alignPivot($this->position),
$imagesize->alignPivot($this->position),
$this->position
);
}

View File

@@ -94,7 +94,7 @@ class Resizer
public function toWidth(int $width): self
{
return $this->height($width);
return $this->width($width);
}
public function toHeight(int $height): self