Skip to content

Commit

Permalink
Restructure and change pad() & padDown() methods
Browse files Browse the repository at this point in the history
- padDown() no longer exists
- pad() does not upscale the original image
- new method contain() which does the same as pad() but is able to upscale
  • Loading branch information
olivervogel committed Dec 3, 2023
1 parent 987367d commit 865adc5
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 238 deletions.
81 changes: 81 additions & 0 deletions src/Drivers/Gd/Modifiers/ContainModifier.php
@@ -0,0 +1,81 @@
<?php

namespace Intervention\Image\Drivers\Gd\Modifiers;

use Intervention\Image\Drivers\DriverModifier;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Modifiers\FillModifier;

/**
* @method SizeInterface getCropSize(ImageInterface $image)
* @method SizeInterface getResizeSize(ImageInterface $image)
* @property int $width
* @property int $height
* @property mixed $background
* @property string $position
*/
class ContainModifier extends DriverModifier
{
public function apply(ImageInterface $image): ImageInterface
{
$crop = $this->getCropSize($image);
$resize = $this->getResizeSize($image);
$background = $this->driver()->handleInput($this->background);

foreach ($image as $frame) {
$this->modify($frame, $crop, $resize, $background);
}

return $image;
}

protected function modify(
FrameInterface $frame,
SizeInterface $crop,
SizeInterface $resize,
ColorInterface $background
): void {
// create new gd image
$modified = $this->driver()->createImage(
$resize->width(),
$resize->height()
)->modify(
new FillModifier($background)
)->core()->native();

// make image area transparent to keep transparency
// even if background-color is set
$transparent = imagecolorallocatealpha($modified, 255, 0, 255, 127);
imagealphablending($modified, false); // do not blend / just overwrite
imagecolortransparent($modified, $transparent);
imagefilledrectangle(
$modified,
$crop->pivot()->x(),
$crop->pivot()->y(),
$crop->pivot()->x() + $crop->width() - 1,
$crop->pivot()->y() + $crop->height() - 1,
$transparent
);

// copy image from original with blending alpha
imagealphablending($modified, true);
imagecopyresampled(
$modified,
$frame->native(),
$crop->pivot()->x(),
$crop->pivot()->y(),
0,
0,
$crop->width(),
$crop->height(),
$frame->size()->width(),
$frame->size()->height()
);

// set new content as recource
$frame->setNative($modified);
}
}
7 changes: 0 additions & 7 deletions src/Drivers/Gd/Modifiers/PadDownModifier.php

This file was deleted.

76 changes: 1 addition & 75 deletions src/Drivers/Gd/Modifiers/PadModifier.php
Expand Up @@ -2,80 +2,6 @@

namespace Intervention\Image\Drivers\Gd\Modifiers;

use Intervention\Image\Drivers\DriverModifier;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Modifiers\FillModifier;

/**
* @method SizeInterface getCropSize(ImageInterface $image)
* @method SizeInterface getResizeSize(ImageInterface $image)
* @property int $width
* @property int $height
* @property mixed $background
* @property string $position
*/
class PadModifier extends DriverModifier
class PadModifier extends ContainModifier
{
public function apply(ImageInterface $image): ImageInterface
{
$crop = $this->getCropSize($image);
$resize = $this->getResizeSize($image);
$background = $this->driver()->handleInput($this->background);

foreach ($image as $frame) {
$this->modify($frame, $crop, $resize, $background);
}

return $image;
}

protected function modify(
FrameInterface $frame,
SizeInterface $crop,
SizeInterface $resize,
ColorInterface $background
): void {
// create new gd image
$modified = $this->driver()->createImage(
$resize->width(),
$resize->height()
)->modify(
new FillModifier($background)
)->core()->native();

// make image area transparent to keep transparency
// even if background-color is set
$transparent = imagecolorallocatealpha($modified, 255, 0, 255, 127);
imagealphablending($modified, false); // do not blend / just overwrite
imagecolortransparent($modified, $transparent);
imagefilledrectangle(
$modified,
$crop->pivot()->x(),
$crop->pivot()->y(),
$crop->pivot()->x() + $crop->width() - 1,
$crop->pivot()->y() + $crop->height() - 1,
$transparent
);

// copy image from original with blending alpha
imagealphablending($modified, true);
imagecopyresampled(
$modified,
$frame->native(),
$crop->pivot()->x(),
$crop->pivot()->y(),
0,
0,
$crop->width(),
$crop->height(),
$frame->size()->width(),
$frame->size()->height()
);

// set new content as recource
$frame->setNative($modified);
}
}
90 changes: 90 additions & 0 deletions src/Drivers/Imagick/Modifiers/ContainModifier.php
@@ -0,0 +1,90 @@
<?php

namespace Intervention\Image\Drivers\Imagick\Modifiers;

use ImagickDraw;
use ImagickPixel;
use Intervention\Image\Drivers\DriverModifier;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;

/**
* @method SizeInterface getCropSize(ImageInterface $image)
* @method SizeInterface getResizeSize(ImageInterface $image)
* @property int $width
* @property int $height
* @property mixed $background
* @property string $position
*/
class ContainModifier extends DriverModifier
{
public function apply(ImageInterface $image): ImageInterface
{

$crop = $this->getCropSize($image);
$resize = $this->getResizeSize($image);
$transparent = new ImagickPixel('transparent');
$background = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->background)
);

foreach ($image as $frame) {
$frame->native()->scaleImage(
$crop->width(),
$crop->height(),
);

$frame->native()->setBackgroundColor($transparent);
$frame->native()->setImageBackgroundColor($transparent);

$frame->native()->extentImage(
$resize->width(),
$resize->height(),
$crop->pivot()->x() * -1,
$crop->pivot()->y() * -1
);

if ($resize->width() > $crop->width()) {
// fill new emerged background
$draw = new ImagickDraw();
$draw->setFillColor($background);
$draw->rectangle(
0,
0,
$crop->pivot()->x() - 1,
$resize->height()
);
$frame->native()->drawImage($draw);
$draw->rectangle(
$crop->pivot()->x() + $crop->width(),
0,
$resize->width(),
$resize->height()
);
$frame->native()->drawImage($draw);
}

if ($resize->height() > $crop->height()) {
// fill new emerged background
$draw = new ImagickDraw();
$draw->setFillColor($background);
$draw->rectangle(
0,
0,
$resize->width(),
$crop->pivot()->y() - 1
);
$frame->native()->drawImage($draw);
$draw->rectangle(
0,
$crop->pivot()->y() + $crop->height(),
$resize->width(),
$resize->height()
);
$frame->native()->drawImage($draw);
}
}

return $image;
}
}
7 changes: 0 additions & 7 deletions src/Drivers/Imagick/Modifiers/PadDownModifier.php

This file was deleted.

85 changes: 1 addition & 84 deletions src/Drivers/Imagick/Modifiers/PadModifier.php
Expand Up @@ -2,89 +2,6 @@

namespace Intervention\Image\Drivers\Imagick\Modifiers;

use ImagickDraw;
use ImagickPixel;
use Intervention\Image\Drivers\DriverModifier;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;

/**
* @method SizeInterface getCropSize(ImageInterface $image)
* @method SizeInterface getResizeSize(ImageInterface $image)
* @property int $width
* @property int $height
* @property mixed $background
* @property string $position
*/
class PadModifier extends DriverModifier
class PadModifier extends ContainModifier
{
public function apply(ImageInterface $image): ImageInterface
{

$crop = $this->getCropSize($image);
$resize = $this->getResizeSize($image);
$transparent = new ImagickPixel('transparent');
$background = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->background)
);

foreach ($image as $frame) {
$frame->native()->scaleImage(
$crop->width(),
$crop->height(),
);

$frame->native()->setBackgroundColor($transparent);
$frame->native()->setImageBackgroundColor($transparent);

$frame->native()->extentImage(
$resize->width(),
$resize->height(),
$crop->pivot()->x() * -1,
$crop->pivot()->y() * -1
);

if ($resize->width() > $crop->width()) {
// fill new emerged background
$draw = new ImagickDraw();
$draw->setFillColor($background);
$draw->rectangle(
0,
0,
$crop->pivot()->x() - 1,
$resize->height()
);
$frame->native()->drawImage($draw);
$draw->rectangle(
$crop->pivot()->x() + $crop->width(),
0,
$resize->width(),
$resize->height()
);
$frame->native()->drawImage($draw);
}

if ($resize->height() > $crop->height()) {
// fill new emerged background
$draw = new ImagickDraw();
$draw->setFillColor($background);
$draw->rectangle(
0,
0,
$crop->width(),
$crop->pivot()->y() - 1
);
$frame->native()->drawImage($draw);
$draw->rectangle(
0,
$crop->pivot()->y() + $crop->height(),
$resize->width(),
$resize->height()
);
$frame->native()->drawImage($draw);
}
}

return $image;
}
}
5 changes: 5 additions & 0 deletions src/Geometry/Rectangle.php
Expand Up @@ -223,4 +223,9 @@ public function contain(int $width, int $height): SizeInterface
{
return $this->resizer($width, $height)->contain($this);
}

public function containMax(int $width, int $height): SizeInterface
{
return $this->resizer($width, $height)->containDown($this);
}
}

0 comments on commit 865adc5

Please sign in to comment.