mirror of
https://github.com/Intervention/image.git
synced 2025-09-09 21:50:47 +02:00
FitModifier
This commit is contained in:
@@ -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
|
public function place($element, string $position = 'top-left', int $offset_x = 0, int $offset_y = 0): ImageInterface
|
||||||
{
|
{
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
|
22
src/Drivers/Gd/Modifiers/FitDownModifier.php
Normal file
22
src/Drivers/Gd/Modifiers/FitDownModifier.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@@ -10,18 +10,6 @@ use Intervention\Image\Interfaces\ModifierInterface;
|
|||||||
use Intervention\Image\Interfaces\SizeInterface;
|
use Intervention\Image\Interfaces\SizeInterface;
|
||||||
use Intervention\Image\Traits\CanResizeGeometrically;
|
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
|
class FitModifier extends ResizeModifier implements ModifierInterface
|
||||||
{
|
{
|
||||||
use CanResizeGeometrically;
|
use CanResizeGeometrically;
|
||||||
@@ -49,13 +37,22 @@ class FitModifier extends ResizeModifier implements ModifierInterface
|
|||||||
|
|
||||||
protected function getCropSize(ImageInterface $image): SizeInterface
|
protected function getCropSize(ImageInterface $image): SizeInterface
|
||||||
{
|
{
|
||||||
|
$imagesize = $image->getSize();
|
||||||
|
|
||||||
|
// auto height
|
||||||
$size = $this->resizeGeometrically($this->target)
|
$size = $this->resizeGeometrically($this->target)
|
||||||
->toWidth($image->width())
|
->toWidth($imagesize->getWidth())
|
||||||
->toHeight($image->height())
|
|
||||||
->scale();
|
->scale();
|
||||||
|
|
||||||
|
if (!$size->fitsInto($imagesize)) {
|
||||||
|
// auto width
|
||||||
|
$size = $this->resizeGeometrically($this->target)
|
||||||
|
->toHeight($imagesize->getHeight())
|
||||||
|
->scale();
|
||||||
|
}
|
||||||
|
|
||||||
return $size->alignPivotTo(
|
return $size->alignPivotTo(
|
||||||
$image->getSize()->alignPivot($this->position),
|
$imagesize->alignPivot($this->position),
|
||||||
$this->position
|
$this->position
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -94,7 +94,7 @@ class Resizer
|
|||||||
|
|
||||||
public function toWidth(int $width): self
|
public function toWidth(int $width): self
|
||||||
{
|
{
|
||||||
return $this->height($width);
|
return $this->width($width);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toHeight(int $height): self
|
public function toHeight(int $height): self
|
||||||
|
Reference in New Issue
Block a user