From e098d25701149ae13d19d50de59c165aec3b61fd Mon Sep 17 00:00:00 2001 From: phmg701 <68058405+phmg701@users.noreply.github.com> Date: Tue, 20 Apr 2021 10:40:10 +0200 Subject: [PATCH 1/2] Update ImageMediaTrait.php Add needed magic actions to enable image frame creation. --- system/src/Grav/Common/Media/Traits/ImageMediaTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Media/Traits/ImageMediaTrait.php b/system/src/Grav/Common/Media/Traits/ImageMediaTrait.php index 6181c941eb..06056ebc61 100644 --- a/system/src/Grav/Common/Media/Traits/ImageMediaTrait.php +++ b/system/src/Grav/Common/Media/Traits/ImageMediaTrait.php @@ -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 */ From ddeaeea9382cf563e604c3b0a75aa09bfece21ae Mon Sep 17 00:00:00 2001 From: phmg701 <68058405+phmg701@users.noreply.github.com> Date: Tue, 20 Apr 2021 10:44:04 +0200 Subject: [PATCH 2/2] Update ImageMedium.php Adds addFrame method that can be (recursively) called from Twig. --- .../Grav/Common/Page/Medium/ImageMedium.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/system/src/Grav/Common/Page/Medium/ImageMedium.php b/system/src/Grav/Common/Page/Medium/ImageMedium.php index 60d49977b3..70d13f8fdb 100644 --- a/system/src/Grav/Common/Page/Medium/ImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/ImageMedium.php @@ -337,6 +337,37 @@ public function cropZoom() 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 @@ public function cropZoom() * @param mixed $args * @return $this|mixed */ + public function __call($method, $args) { if (!in_array($method, static::$magic_actions, true)) {