mirror of
https://github.com/getgrav/grav.git
synced 2025-12-05 15:29:57 +01:00
Compare commits
4 Commits
3ddc548d51
...
feature/wa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
883c935e05 | ||
|
|
920127837f | ||
|
|
de7b3126f7 | ||
|
|
82a1172e6e |
@@ -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
BIN
system/images/watermark.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 94 KiB |
@@ -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')]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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  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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user