Skip to content

Commit

Permalink
Simplify ResizeCanvasModifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Jan 11, 2025
1 parent bcc055f commit 0b8be3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 131 deletions.
70 changes: 8 additions & 62 deletions src/Drivers/Gd/Modifiers/ResizeCanvasModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@

namespace Intervention\Image\Drivers\Gd\Modifiers;

use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\ResizeCanvasModifier as GenericResizeCanvasModifier;

Expand All @@ -25,62 +17,16 @@ class ResizeCanvasModifier extends GenericResizeCanvasModifier implements Specia
*/
public function apply(ImageInterface $image): ImageInterface
{
$resize = $this->cropSize($image);
$background = $this->driver()->handleInput($this->background);
$cropSize = $this->cropSize($image);

foreach ($image as $frame) {
$this->modify($frame, $resize, $background);
}
$image->modify(new CropModifier(
$cropSize->width(),
$cropSize->height(),
$cropSize->pivot()->x(),
$cropSize->pivot()->y(),
$this->background,
));

return $image;
}

/**
* @throws ColorException
*/
protected function modify(
FrameInterface $frame,
SizeInterface $resize,
ColorInterface $background,
): void {
// create new canvas with target size & transparent background color
$modified = Cloner::cloneEmpty($frame->native(), $resize, $background);

// make image area transparent to keep transparency
// even if background-color is set
$transparent = imagecolorallocatealpha(
$modified,
$background->channel(Red::class)->value(),
$background->channel(Green::class)->value(),
$background->channel(Blue::class)->value(),
127,
);

// create transparent area to place the original on top
imagealphablending($modified, false); // do not blend / just overwrite
imagecolortransparent($modified, $transparent);
imagefilledrectangle(
$modified,
$resize->pivot()->x() * -1,
$resize->pivot()->y() * -1,
abs($resize->pivot()->x()) + $frame->size()->width() - 1,
abs($resize->pivot()->y()) + $frame->size()->height() - 1,
$transparent,
);

// place original
imagecopy(
$modified,
$frame->native(),
$resize->pivot()->x() * -1,
$resize->pivot()->y() * -1,
0,
0,
$frame->size()->width(),
$frame->size()->height(),
);

// set new content as resource
$frame->setNative($modified);
}
}
78 changes: 9 additions & 69 deletions src/Drivers/Imagick/Modifiers/ResizeCanvasModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Intervention\Image\Drivers\Imagick\Modifiers;

use ImagickDraw;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\ResizeCanvasModifier as GenericResizeCanvasModifier;
Expand All @@ -13,74 +12,15 @@ class ResizeCanvasModifier extends GenericResizeCanvasModifier implements Specia
{
public function apply(ImageInterface $image): ImageInterface
{
$size = $image->size();
$resize = $this->cropSize($image);

$background = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->background)
);

foreach ($image as $frame) {
$frame->native()->extentImage(
$resize->width(),
$resize->height(),
$resize->pivot()->x(),
$resize->pivot()->y()
);

if ($resize->width() > $size->width()) {
// fill new emerged background
$draw = new ImagickDraw();
$draw->setFillColor($background);

$delta_width = abs($resize->pivot()->x());
$delta_height = $resize->pivot()->y() * -1;

if ($delta_width > 0) {
$draw->rectangle(
0,
$delta_height,
$delta_width - 1,
$delta_height + $size->height() - 1
);
}

$draw->rectangle(
$size->width() + $delta_width,
$delta_height,
$resize->width(),
$delta_height + $size->height() - 1
);

$frame->native()->drawImage($draw);
}

if ($resize->height() > $size->height()) {
// fill new emerged background
$draw = new ImagickDraw();
$draw->setFillColor($background);

$delta = abs($resize->pivot()->y());

if ($delta > 0) {
$draw->rectangle(
0,
0,
$resize->width(),
$delta - 1
);
}

$draw->rectangle(
0,
$size->height() + $delta,
$resize->width(),
$resize->height()
);

$frame->native()->drawImage($draw);
}
}
$cropSize = $this->cropSize($image);

$image->modify(new CropModifier(
$cropSize->width(),
$cropSize->height(),
$cropSize->pivot()->x(),
$cropSize->pivot()->y(),
$this->background,
));

return $image;
}
Expand Down

0 comments on commit 0b8be3d

Please sign in to comment.