mirror of
https://github.com/getgrav/grav.git
synced 2026-02-27 00:51:30 +01:00
Adding the addFrame method to ImageMedium (#3323)
* Update ImageMediaTrait.php Add needed magic actions to enable image frame creation. * Update ImageMedium.php Adds addFrame method that can be (recursively) called from Twig.
This commit is contained in:
@@ -56,7 +56,7 @@ trait ImageMediaTrait
|
||||
'resize', 'forceResize', 'cropResize', 'crop', 'zoomCrop',
|
||||
'negate', 'brightness', 'contrast', 'grayscale', 'emboss',
|
||||
'smooth', 'sharp', 'edge', 'colorize', 'sepia', 'enableProgressive',
|
||||
'rotate', 'flip', 'fixOrientation', 'gaussianBlur', 'format'
|
||||
'rotate', 'flip', 'fixOrientation', 'gaussianBlur', 'format', 'create', 'fill', 'merge'
|
||||
];
|
||||
|
||||
/** @var array */
|
||||
|
||||
@@ -337,6 +337,37 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a frame to image
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addFrame(int $border = 10, string $color = '0x000000')
|
||||
{
|
||||
if(is_int(intval($border)) && $border>0 && preg_match('/^0x[a-f0-9]{6}$/i', $color)) { // $border must be an integer and bigger than 0; $color must be formatted as an HEX value (0x??????).
|
||||
$image = ImageFile::open($this->path());
|
||||
}
|
||||
else {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$dst_width = $image->width()+2*$border;
|
||||
$dst_height = $image->height()+2*$border;
|
||||
|
||||
$frame = ImageFile::create($dst_width, $dst_height);
|
||||
|
||||
$frame->__call('fill', [$color]);
|
||||
|
||||
$this->image = $frame;
|
||||
|
||||
$this->__call('merge', [$image, $border, $border]);
|
||||
|
||||
$this->saveImage();
|
||||
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward the call to the image processing method.
|
||||
*
|
||||
@@ -344,6 +375,7 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate
|
||||
* @param mixed $args
|
||||
* @return $this|mixed
|
||||
*/
|
||||
|
||||
public function __call($method, $args)
|
||||
{
|
||||
if (!in_array($method, static::$magic_actions, true)) {
|
||||
|
||||
Reference in New Issue
Block a user