Watermark Media Action (#3308)

* added gregwar merge media action and watermark media function

* remove the dump left by accident

* added scaling to the watermark

Co-authored-by: Ricardo <ricardo@urbansquid.london>
Co-authored-by: Andy Miller <1084697+rhukster@users.noreply.github.com>
This commit is contained in:
Ricardo Verdugo
2021-10-21 13:31:33 +01:00
committed by GitHub
parent 8b0a6906c7
commit fd61f82f5a
4 changed files with 76 additions and 0 deletions

View File

@@ -162,6 +162,12 @@ images:
retina_scale: 1 # scale to adjust auto-sizes for better handling of HiDPI resolutions
defaults:
loading: auto # Let browser pick [auto|lazy|eager]
watermark:
image: 'system://images/watermark.png' # Path to a watermark image
position_y: 'center' # top|center|bottom
position_x: 'center' # left|center|right
scale: 33 # percentage of watermark scale
watermark_all: false # automatically watermark all images
media:
enable_media_timestamp: false # Enable media timestamps

BIN
system/images/watermark.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

View File

@@ -50,6 +50,8 @@ trait ImageMediaTrait
/** @var integer */
protected $retina_scale;
/** @var bool */
protected $watermark;
/** @var array */
public static $magic_actions = [
@@ -379,6 +381,8 @@ trait ImageMediaTrait
$this->aspect_ratio = $config->get('system.images.cls.aspect_ratio', false);
$this->retina_scale = $config->get('system.images.cls.retina_scale', 1);
$this->watermark = $config->get('system.images.watermark.watermark_all', false);
return $this;
}
@@ -415,6 +419,10 @@ trait ImageMediaTrait
$this->image->merge(ImageFile::open($overlay));
}
if ($this->watermark) {
$this->watermark();
}
return $this->image->cacheFile($this->format, $this->quality, false, [$this->get('width'), $this->get('height'), $this->get('modified')]);
}
}

View File

@@ -17,6 +17,7 @@ use Grav\Common\Media\Interfaces\MediaLinkInterface;
use Grav\Common\Media\Traits\ImageLoadingTrait;
use Grav\Common\Media\Traits\ImageMediaTrait;
use Grav\Common\Utils;
use Gregwar\Image\Image;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use function func_get_args;
use function in_array;
@@ -325,6 +326,67 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate
return $this;
}
public function watermark($image = null, $position = null, $scale = null)
{
$grav = $this->getGrav();
$locator = $grav['locator'];
$config = $grav['config'];
$args = func_get_args();
$file = $args[0] ?? '1'; // using '1' because of markdown. doing ![](image.jpg?watermark) returns $args[0]='1';
$file = $file === '1' ? $config->get('system.images.watermark.image') : $args[0];
$watermark = $locator->findResource($file);
$watermark = ImageFile::open($watermark);
// Scaling operations
$scale = ($scale ?? $config->get('system.images.watermark.scale', 100)) / 100;
$wwidth = $this->get('width') * $scale;
$wheight = $this->get('height') * $scale;
$watermark->resize($wwidth, $wheight);
// Position operations
$position = !empty($args[1]) ? explode('-', $args[1]) : ['center', 'center']; // todo change to config
$positionY = $position[0] ?? $config->get('system.images.watermark.position_y', 'center');
$positionX = $position[1] ?? $config->get('system.images.watermark.position_x', 'center');
switch ($positionY)
{
case 'top':
$positionY = 0;
break;
case 'bottom':
$positionY = $this->get('height')-$wheight;
break;
case 'center':
$positionY = ($this->get('height')/2) - ($wheight/2);
break;
}
switch ($positionX)
{
case 'left':
$positionX = 0;
break;
case 'right':
$positionX = $this->get('width')-$wwidth;
break;
case 'center':
$positionX = ($this->get('width')/2) - ($wwidth/2);
break;
}
$this->__call('merge', [$watermark,$positionX, $positionY]);
return $this;
}
/**
* Handle this commonly used variant
*