From 18d7fd4c7d1e8af005336fd93dbfe536d77dc818 Mon Sep 17 00:00:00 2001 From: phmg701 <68058405+phmg701@users.noreply.github.com> Date: Wed, 2 Jun 2021 00:38:40 +0200 Subject: [PATCH] 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. --- .../Common/Media/Traits/ImageMediaTrait.php | 2 +- .../Grav/Common/Page/Medium/ImageMedium.php | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Media/Traits/ImageMediaTrait.php b/system/src/Grav/Common/Media/Traits/ImageMediaTrait.php index 6181c941e..06056ebc6 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 */ diff --git a/system/src/Grav/Common/Page/Medium/ImageMedium.php b/system/src/Grav/Common/Page/Medium/ImageMedium.php index 60d49977b..70d13f8fd 100644 --- a/system/src/Grav/Common/Page/Medium/ImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/ImageMedium.php @@ -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)) {