-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure and change pad() & padDown() methods
- 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
1 parent
987367d
commit 865adc5
Showing
16 changed files
with
270 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.