1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-30 09:10:21 +02:00

Resize Modifiers

This commit is contained in:
Oliver Vogel
2021-11-28 09:45:34 +01:00
parent e643de053b
commit b649a6751e
3 changed files with 91 additions and 16 deletions

View File

@@ -208,7 +208,7 @@ abstract class AbstractImage
$crop = $imagesize->contain($resize)->alignPivotTo($resize, $position);
return $this->modify(
$this->resolveDriverClass('Modifiers\FitModifier', $crop, $resize, $backgroundColor)
$this->resolveDriverClass('Modifiers\PadModifier', $crop, $resize, $backgroundColor)
);
}
@@ -223,7 +223,7 @@ abstract class AbstractImage
return $this->modify(
$this->resolveDriverClass('Modifiers\FitModifier', $crop, $resize, $backgroundColor)
$this->resolveDriverClass('Modifiers\PadModifier', $crop, $resize, $backgroundColor)
);
}

View File

@@ -11,17 +11,13 @@ use Intervention\Image\Traits\CanResizeGeometrically;
class FitModifier implements ModifierInterface
{
use CanHandleInput;
protected $crop;
protected $resize;
protected $backgroundColor;
public function __construct(SizeInterface $crop, SizeInterface $resize, $backgroundColor = null)
public function __construct(SizeInterface $crop, SizeInterface $resize)
{
$this->crop = $crop;
$this->resize = $resize;
$this->backgroundColor = $backgroundColor;
}
public function apply(ImageInterface $image): ImageInterface
@@ -41,10 +37,6 @@ class FitModifier implements ModifierInterface
$this->resize->getHeight()
);
$color = $this->handleInput($this->backgroundColor);
imagefill($modified, 0, 0, $color->toInt());
// get current image
$current = $frame->getCore();
@@ -65,14 +57,14 @@ class FitModifier implements ModifierInterface
imagecopyresampled(
$modified,
$current,
0,
0,
$this->crop->getPivot()->getX(),
$this->crop->getPivot()->getY(),
0,
0,
$this->resize->getWidth(),
$this->resize->getHeight(),
$this->crop->getWidth(),
$this->crop->getHeight(),
$frame->getSize()->getWidth(),
$frame->getSize()->getHeight()
$this->crop->getHeight()
);
imagedestroy($current);

View File

@@ -0,0 +1,83 @@
<?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\CanHandleInput;
use Intervention\Image\Traits\CanResizeGeometrically;
class PadModifier implements ModifierInterface
{
use CanHandleInput;
protected $crop;
protected $resize;
protected $backgroundColor;
public function __construct(SizeInterface $crop, SizeInterface $resize, $backgroundColor = null)
{
$this->crop = $crop;
$this->resize = $resize;
$this->backgroundColor = $backgroundColor;
}
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()
);
$color = $this->handleInput($this->backgroundColor);
imagefill($modified, 0, 0, $color->toInt());
// 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->crop->getPivot()->getX(),
$this->crop->getPivot()->getY(),
0,
0,
$this->crop->getWidth(),
$this->crop->getHeight(),
$frame->getSize()->getWidth(),
$frame->getSize()->getHeight()
);
imagedestroy($current);
// set new content as recource
$frame->setCore($modified);
}
}