mirror of
https://github.com/Intervention/image.git
synced 2025-08-29 16:50:07 +02:00
refactoring
This commit is contained in:
@@ -168,8 +168,10 @@ abstract class AbstractImage
|
|||||||
|
|
||||||
public function fit(int $width, int $height, string $position = 'center'): ImageInterface
|
public function fit(int $width, int $height, string $position = 'center'): ImageInterface
|
||||||
{
|
{
|
||||||
|
$size = new Size($width, $height);
|
||||||
|
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $width, $height, $position)
|
$this->resolveDriverClass('Modifiers\CropResizeModifier', $size, $position)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,10 +3,12 @@
|
|||||||
namespace Intervention\Image\Drivers\Gd\Modifiers;
|
namespace Intervention\Image\Drivers\Gd\Modifiers;
|
||||||
|
|
||||||
use Intervention\Image\Geometry\Resizer;
|
use Intervention\Image\Geometry\Resizer;
|
||||||
|
use Intervention\Image\Geometry\Size;
|
||||||
use Intervention\Image\Interfaces\FrameInterface;
|
use Intervention\Image\Interfaces\FrameInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
use Intervention\Image\Interfaces\ModifierInterface;
|
use Intervention\Image\Interfaces\ModifierInterface;
|
||||||
use Intervention\Image\Interfaces\SizeInterface;
|
use Intervention\Image\Interfaces\SizeInterface;
|
||||||
|
use Intervention\Image\Traits\CanResizeGeometrically;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
@@ -22,48 +24,48 @@ use Intervention\Image\Interfaces\SizeInterface;
|
|||||||
|
|
||||||
class CropResizeModifier implements ModifierInterface
|
class CropResizeModifier implements ModifierInterface
|
||||||
{
|
{
|
||||||
protected $width;
|
use CanResizeGeometrically;
|
||||||
protected $height;
|
|
||||||
|
protected $target;
|
||||||
protected $position;
|
protected $position;
|
||||||
|
|
||||||
public function __construct(int $width, int $height, string $position)
|
public function __construct(SizeInterface $target, string $position = 'top-left')
|
||||||
{
|
{
|
||||||
$this->width = $width;
|
$this->target = $target;
|
||||||
$this->height = $height;
|
|
||||||
$this->position = $position;
|
$this->position = $position;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function apply(ImageInterface $image): ImageInterface
|
public function apply(ImageInterface $image): ImageInterface
|
||||||
{
|
{
|
||||||
echo "<pre>";
|
$crop = $this->getCropSize($image);
|
||||||
var_dump($this->getCropSize($image));
|
$resize = $this->getResizeSize($image);
|
||||||
var_dump($this->getResizeSize($image));
|
|
||||||
echo "</pre>";
|
|
||||||
exit;
|
|
||||||
|
|
||||||
// foreach ($image as $frame) {
|
foreach ($image as $frame) {
|
||||||
// $this->modify($frame);
|
$this->modify($frame, $crop, $resize);
|
||||||
// }
|
}
|
||||||
|
|
||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getCropSize(ImageInterface $image): SizeInterface
|
protected function getCropSize(ImageInterface $image): SizeInterface
|
||||||
{
|
{
|
||||||
$resizer = new Resizer(new Size($this->width, $this->height));
|
$size = $this->resizeGeometrically($this->target)
|
||||||
$resizer->width($image->width());
|
->toWidth($image->width())
|
||||||
$resizer->height($image->height());
|
->toHeight($image->height())
|
||||||
|
->scale();
|
||||||
|
|
||||||
return $resizer->scale()->align($this->position);
|
return $size->alignPivotTo(
|
||||||
|
$image->getSize()->alignPivot($this->position),
|
||||||
|
$this->position
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getResizeSize(ImageInterface $image): SizeInterface
|
protected function getResizeSize(ImageInterface $image): SizeInterface
|
||||||
{
|
{
|
||||||
$resizer = new Resizer($this->getCropSize($image));
|
return $this->resizeGeometrically($this->getCropSize($image))
|
||||||
$resizer->width($this->width);
|
->toWidth($this->target->getWidth())
|
||||||
$resizer->height($this->height);
|
->toHeight($this->target->getHeight())
|
||||||
|
->scale();
|
||||||
return $resizer->scale()->align($this->position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,12 +82,12 @@ class CropResizeModifier implements ModifierInterface
|
|||||||
* @param int $src_h
|
* @param int $src_h
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function modify(FrameInterface $frame): void
|
protected function modify(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void
|
||||||
{
|
{
|
||||||
// create new image
|
// create new image
|
||||||
$modified = imagecreatetruecolor(
|
$modified = imagecreatetruecolor(
|
||||||
$this->resizeTo->getWidth(),
|
$resize->getWidth(),
|
||||||
$this->resizeTo->getHeight()
|
$resize->getHeight()
|
||||||
);
|
);
|
||||||
|
|
||||||
// get current image
|
// get current image
|
||||||
@@ -108,14 +110,14 @@ class CropResizeModifier implements ModifierInterface
|
|||||||
$result = imagecopyresampled(
|
$result = imagecopyresampled(
|
||||||
$modified,
|
$modified,
|
||||||
$gd,
|
$gd,
|
||||||
$this->resizeTo->getPivot()->getX(),
|
$resize->getPivot()->getX(),
|
||||||
$this->resizeTo->getPivot()->getY(),
|
$resize->getPivot()->getY(),
|
||||||
$this->cropTo->getPivot()->getX(),
|
$crop->getPivot()->getX(),
|
||||||
$this->cropTo->getPivot()->getY(),
|
$crop->getPivot()->getY(),
|
||||||
$this->resizeTo->getWidth(),
|
$resize->getWidth(),
|
||||||
$this->resizeTo->getHeight(),
|
$resize->getHeight(),
|
||||||
$this->cropTo->getWidth(),
|
$crop->getWidth(),
|
||||||
$this->cropTo->getHeight()
|
$crop->getHeight()
|
||||||
);
|
);
|
||||||
|
|
||||||
imagedestroy($gd);
|
imagedestroy($gd);
|
||||||
|
@@ -92,6 +92,16 @@ class Resizer
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function toWidth(int $width): self
|
||||||
|
{
|
||||||
|
return $this->height($width);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toHeight(int $height): self
|
||||||
|
{
|
||||||
|
return $this->height($height);
|
||||||
|
}
|
||||||
|
|
||||||
public function setTargetSizeByArray(array $arguments): self
|
public function setTargetSizeByArray(array $arguments): self
|
||||||
{
|
{
|
||||||
if (isset($arguments[0]) && is_callable($arguments[0])) {
|
if (isset($arguments[0]) && is_callable($arguments[0])) {
|
||||||
|
14
src/Traits/CanResizeGeometrically.php
Normal file
14
src/Traits/CanResizeGeometrically.php
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Traits;
|
||||||
|
|
||||||
|
use Intervention\Image\Geometry\Resizer;
|
||||||
|
use Intervention\Image\Interfaces\SizeInterface;
|
||||||
|
|
||||||
|
trait CanResizeGeometrically
|
||||||
|
{
|
||||||
|
public function resizeGeometrically(SizeInterface $size): Resizer
|
||||||
|
{
|
||||||
|
return new Resizer($size);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user