Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Oct 22, 2021
2 parents 7154775 + fd61f82 commit 0510165
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions system/config/system.yaml
Expand Up @@ -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
Expand Down
Binary file added system/images/watermark.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions system/src/Grav/Common/Media/Traits/ImageMediaTrait.php
Expand Up @@ -50,6 +50,8 @@ trait ImageMediaTrait
/** @var integer */
protected $retina_scale;

/** @var bool */
protected $watermark;

/** @var array */
public static $magic_actions = [
Expand Down Expand Up @@ -379,6 +381,8 @@ protected function image()
$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;
}

Expand Down Expand Up @@ -415,6 +419,10 @@ protected function saveImage()
$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')]);
}
}
62 changes: 62 additions & 0 deletions system/src/Grav/Common/Page/Medium/ImageMedium.php
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -325,6 +326,67 @@ public function retinaScale($scale = 1)
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
*
Expand Down

0 comments on commit 0510165

Please sign in to comment.