mirror of
https://github.com/Intervention/image.git
synced 2025-02-24 14:12:40 +01:00
ResizeModifier work
This commit is contained in:
parent
8aa562816d
commit
16b1fd800c
@ -127,125 +127,103 @@ abstract class AbstractImage
|
|||||||
|
|
||||||
public function resize(...$arguments): ImageInterface
|
public function resize(...$arguments): ImageInterface
|
||||||
{
|
{
|
||||||
$crop = $this->getSize();
|
$resized = Resizer::make()->setTargetSizeByArray($arguments)
|
||||||
$resize = Resizer::make()
|
->resize($this->getSize());
|
||||||
->setTargetSizeByArray($arguments)
|
|
||||||
->resize($crop);
|
|
||||||
|
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $crop, $resize)
|
$this->resolveDriverClass('Modifiers\ResizeModifier', $resized)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resizeDown(...$arguments): ImageInterface
|
public function resizeDown(...$arguments): ImageInterface
|
||||||
{
|
{
|
||||||
$crop = $this->getSize();
|
$resized = Resizer::make()->setTargetSizeByArray($arguments)
|
||||||
$resize = Resizer::make()
|
->resizeDown($this->getSize());
|
||||||
->setTargetSizeByArray($arguments)
|
|
||||||
->resizeDown($crop);
|
|
||||||
|
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $crop, $resize)
|
$this->resolveDriverClass('Modifiers\ResizeModifier', $resized)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scale(...$arguments): ImageInterface
|
public function scale(...$arguments): ImageInterface
|
||||||
{
|
{
|
||||||
$crop = $this->getSize();
|
$resized = Resizer::make()->setTargetSizeByArray($arguments)
|
||||||
$resize = Resizer::make()
|
->scale($this->getSize());
|
||||||
->setTargetSizeByArray($arguments)
|
|
||||||
->scale($crop);
|
|
||||||
|
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $crop, $resize)
|
$this->resolveDriverClass('Modifiers\ResizeModifier', $resized)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scaleDown(...$arguments): ImageInterface
|
public function scaleDown(...$arguments): ImageInterface
|
||||||
{
|
{
|
||||||
$crop = $this->getSize();
|
$resized = Resizer::make()->setTargetSizeByArray($arguments)
|
||||||
$resize = Resizer::make()
|
->scaleDown($this->getSize());
|
||||||
->setTargetSizeByArray($arguments)
|
|
||||||
->scaleDown($crop);
|
|
||||||
|
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $crop, $resize)
|
$this->resolveDriverClass('Modifiers\ResizeModifier', $resized)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fit(int $width, int $height, string $position = 'center'): ImageInterface
|
public function fit(int $width, int $height, string $position = 'center'): ImageInterface
|
||||||
{
|
{
|
||||||
|
// original
|
||||||
$imagesize = $this->getSize();
|
$imagesize = $this->getSize();
|
||||||
|
|
||||||
// crop
|
// crop
|
||||||
$crop = new Size($width, $height);
|
$crop = new Size($width, $height);
|
||||||
$crop = $crop->contain($imagesize)->alignPivotTo(
|
$crop = $crop->contain($imagesize)->alignPivotTo($imagesize, $position);
|
||||||
$imagesize->alignPivot($position),
|
|
||||||
$position
|
|
||||||
);
|
|
||||||
|
|
||||||
// resize
|
// resize
|
||||||
$resize = $crop->scale($width, $height);
|
$resize = $crop->scale($width, $height);
|
||||||
|
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $crop, $resize, $position)
|
$this->resolveDriverClass('Modifiers\FitModifier', $crop, $resize)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface
|
public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface
|
||||||
{
|
{
|
||||||
|
// original
|
||||||
$imagesize = $this->getSize();
|
$imagesize = $this->getSize();
|
||||||
|
|
||||||
// crop
|
// crop
|
||||||
$crop = new Size($width, $height);
|
$crop = new Size($width, $height);
|
||||||
$crop = $crop->contain($imagesize)->alignPivotTo(
|
$crop = $crop->contain($imagesize)->alignPivotTo($imagesize, $position);
|
||||||
$imagesize->alignPivot($position),
|
|
||||||
$position
|
|
||||||
);
|
|
||||||
|
|
||||||
// resize
|
// resize
|
||||||
$resize = $crop->scaleDown($width, $height);
|
$resize = $crop->scaleDown($width, $height);
|
||||||
|
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $crop, $resize, $position)
|
$this->resolveDriverClass('Modifiers\FitModifier', $crop, $resize)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function pad(int $width, int $height, string $position = 'center'): ImageInterface
|
public function pad(int $width, int $height, string $position = 'center', $backgroundColor = 'fff'): ImageInterface
|
||||||
{
|
{
|
||||||
|
// original
|
||||||
$imagesize = $this->getSize();
|
$imagesize = $this->getSize();
|
||||||
|
|
||||||
// crop
|
$resize = new Size($width, $height);
|
||||||
$crop = new Size($width, $height);
|
$crop = $imagesize->contain($resize)->alignPivotTo($resize, $position);
|
||||||
$crop = $crop->cover($imagesize)->alignPivotTo(
|
|
||||||
$imagesize->alignPivot($position),
|
|
||||||
$position
|
|
||||||
);
|
|
||||||
|
|
||||||
// resize
|
|
||||||
$resize = $crop->scale($width, $height);
|
|
||||||
|
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $crop, $resize, $position)
|
$this->resolveDriverClass('Modifiers\FitModifier', $crop, $resize, $backgroundColor)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function padDown(int $width, int $height, string $position = 'center'): ImageInterface
|
public function padDown(int $width, int $height, string $position = 'center', $backgroundColor = 'fff'): ImageInterface
|
||||||
{
|
{
|
||||||
|
// original
|
||||||
$imagesize = $this->getSize();
|
$imagesize = $this->getSize();
|
||||||
|
|
||||||
// crop
|
$resize = new Size($width, $height);
|
||||||
$crop = new Size($width, $height);
|
$resize = $resize->resizeDown($imagesize);
|
||||||
$crop = $crop->cover($imagesize)->alignPivotTo(
|
$crop = $imagesize->contain($resize)->alignPivotTo($resize, $position);
|
||||||
$imagesize->alignPivot($position),
|
|
||||||
$position
|
|
||||||
);
|
|
||||||
|
|
||||||
// resize
|
|
||||||
$resize = $crop->scaleDown($width, $height);
|
|
||||||
|
|
||||||
return $this->modify(
|
return $this->modify(
|
||||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $crop, $resize, $position)
|
$this->resolveDriverClass('Modifiers\FitModifier', $crop, $resize, $backgroundColor)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,43 +6,50 @@ 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\CanHandleInput;
|
||||||
use Intervention\Image\Traits\CanResizeGeometrically;
|
use Intervention\Image\Traits\CanResizeGeometrically;
|
||||||
|
|
||||||
class CropResizeModifier implements ModifierInterface
|
class FitModifier implements ModifierInterface
|
||||||
{
|
{
|
||||||
|
use CanHandleInput;
|
||||||
|
|
||||||
protected $crop;
|
protected $crop;
|
||||||
protected $resize;
|
protected $resize;
|
||||||
protected $position;
|
protected $backgroundColor;
|
||||||
|
|
||||||
public function __construct(SizeInterface $crop, SizeInterface $resize, string $position = 'top-left')
|
public function __construct(SizeInterface $crop, SizeInterface $resize, $backgroundColor = null)
|
||||||
{
|
{
|
||||||
$this->crop = $crop;
|
$this->crop = $crop;
|
||||||
$this->resize = $resize;
|
$this->resize = $resize;
|
||||||
$this->position = $position;
|
$this->backgroundColor = $backgroundColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function apply(ImageInterface $image): ImageInterface
|
public function apply(ImageInterface $image): ImageInterface
|
||||||
{
|
{
|
||||||
foreach ($image as $frame) {
|
foreach ($image as $frame) {
|
||||||
$this->modify($frame, $this->crop, $this->resize);
|
$this->modify($frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function modify(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void
|
protected function modify(FrameInterface $frame): void
|
||||||
{
|
{
|
||||||
// create new image
|
// create new image
|
||||||
$modified = imagecreatetruecolor(
|
$modified = imagecreatetruecolor(
|
||||||
$resize->getWidth(),
|
$this->resize->getWidth(),
|
||||||
$resize->getHeight()
|
$this->resize->getHeight()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$color = $this->handleInput($this->backgroundColor);
|
||||||
|
|
||||||
|
imagefill($modified, 0, 0, $color->toInt());
|
||||||
|
|
||||||
// get current image
|
// get current image
|
||||||
$gd = $frame->getCore();
|
$current = $frame->getCore();
|
||||||
|
|
||||||
// preserve transparency
|
// preserve transparency
|
||||||
$transIndex = imagecolortransparent($gd);
|
$transIndex = imagecolortransparent($current);
|
||||||
|
|
||||||
if ($transIndex != -1) {
|
if ($transIndex != -1) {
|
||||||
$rgba = imagecolorsforindex($modified, $transIndex);
|
$rgba = imagecolorsforindex($modified, $transIndex);
|
||||||
@ -55,20 +62,20 @@ class CropResizeModifier implements ModifierInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// copy content from resource
|
// copy content from resource
|
||||||
$result = imagecopyresampled(
|
imagecopyresampled(
|
||||||
$modified,
|
$modified,
|
||||||
$gd,
|
$current,
|
||||||
$resize->getPivot()->getX(),
|
$this->crop->getPivot()->getX(),
|
||||||
$resize->getPivot()->getY(),
|
$this->crop->getPivot()->getY(),
|
||||||
$crop->getPivot()->getX(),
|
0,
|
||||||
$crop->getPivot()->getY(),
|
0,
|
||||||
$resize->getWidth(),
|
$this->crop->getWidth(),
|
||||||
$resize->getHeight(),
|
$this->crop->getHeight(),
|
||||||
$crop->getWidth(),
|
$frame->getSize()->getWidth(),
|
||||||
$crop->getHeight()
|
$frame->getSize()->getHeight()
|
||||||
);
|
);
|
||||||
|
|
||||||
imagedestroy($gd);
|
imagedestroy($current);
|
||||||
|
|
||||||
// set new content as recource
|
// set new content as recource
|
||||||
$frame->setCore($modified);
|
$frame->setCore($modified);
|
72
src/Drivers/Gd/Modifiers/ResizeModifier.php
Normal file
72
src/Drivers/Gd/Modifiers/ResizeModifier.php
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Drivers\Gd\Modifiers;
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
protected $resize;
|
||||||
|
|
||||||
|
public function __construct(SizeInterface $resize)
|
||||||
|
{
|
||||||
|
$this->resize = $resize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function apply(ImageInterface $image): ImageInterface
|
||||||
|
{
|
||||||
|
foreach ($image as $frame) {
|
||||||
|
$this->modify($frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $image;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function modify(FrameInterface $frame): void
|
||||||
|
{
|
||||||
|
// create new image
|
||||||
|
$modified = imagecreatetruecolor(
|
||||||
|
$this->resize->getWidth(),
|
||||||
|
$this->resize->getHeight()
|
||||||
|
);
|
||||||
|
|
||||||
|
// get current image
|
||||||
|
$current = $frame->getCore();
|
||||||
|
|
||||||
|
// preserve transparency
|
||||||
|
$transIndex = imagecolortransparent($current);
|
||||||
|
|
||||||
|
if ($transIndex != -1) {
|
||||||
|
$rgba = imagecolorsforindex($modified, $transIndex);
|
||||||
|
$transColor = imagecolorallocatealpha($modified, $rgba['red'], $rgba['green'], $rgba['blue'], 127);
|
||||||
|
imagefill($modified, 0, 0, $transColor);
|
||||||
|
imagecolortransparent($modified, $transColor);
|
||||||
|
} else {
|
||||||
|
imagealphablending($modified, false);
|
||||||
|
imagesavealpha($modified, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy content from resource
|
||||||
|
imagecopyresampled(
|
||||||
|
$modified,
|
||||||
|
$current,
|
||||||
|
$this->resize->getPivot()->getX(),
|
||||||
|
$this->resize->getPivot()->getY(),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
$this->resize->getWidth(),
|
||||||
|
$this->resize->getHeight(),
|
||||||
|
$frame->getSize()->getWidth(),
|
||||||
|
$frame->getSize()->getHeight()
|
||||||
|
);
|
||||||
|
|
||||||
|
imagedestroy($current);
|
||||||
|
|
||||||
|
// set new content as recource
|
||||||
|
$frame->setCore($modified);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user