Is there any solution for adding watermark for images? Found some topics here, but quite old, for version 2.
You could create a file method that stores a watermarked copy:
<?php
use Kirby\Cms\App as Kirby;
use claviska\SimpleImage;
use Kirby\Cms\File;
Kirby::plugin("texnixe/watermark", [
'fileMethods' => [
'watermark' => function($overlay) {
$img = new SimpleImage($this->root());
$img = $img->overlay($overlay, 'bottom right', 1, -10, -10); // change settings as needed
$filename = $this->name() . '-watermark.' . $this->extension();
$img->toFile($this->parent()->root() . '/' . $filename);
return $this->parent()->files()->find($filename);
}
]
]);
Then use in your template like this:
<?php if(($image = $page->file('somefile.jpg')) && ($overlay = $page->image('overlayimage.png'))): ?>
<img src="<?= $image->watermark($overlay->root()) ?>">
<?php endif; ?>
I’m trying to make watermark based on the code you wrote.
<?php
use Kirby\Cms\App as Kirby;
use claviska\SimpleImage;
use Kirby\Cms\File;
Kirby::plugin("texnixe/watermark", [
'fileMethods' => [
'watermark' => function($overlay = NULL) {
if ($overlay === NULL)
$overlay = url(kirby()->url('media') . '/plugins/texnixe/watermark/img/watermark.png');
$filename = $this->name() . '-watermark.' . $this->extension();
$root = kirby()->root('media');
$base = '/pages/' . $this->parent() . '/watermarks/';
$path = $root . $base . $filename;
if (!F::exists($path)) {
$img = new SimpleImage($this->root());
$img = $img->overlay($overlay, 'center center', 1, -0, -0); // change settings as needed
if (!Dir::exists($root . $base))
Dir::make($root . $base);
$img->toFile($path);
}
return new File([ 'filename' => $filename, 'parent' => $this->parent(), 'root' => $path, 'url' => kirby()->url('media') . $base . $filename ]);
},
],
]);
I want to be able to apply this on image and resize it. I guess I’m missing some parameters for File, but I can not find description, which ones are required.
My non working example:
<?= $page->image()->watermark()->resize(800,1200, 90)->url() ?>
Can you help?
You are trying to create the file in the media folder but file object live in the content folder, so the path must point to the parent root.
But I try making it like “resize” works - create file with watermark in media folder, and allow resizing it after it is done. I do not want to touch original object (file).
Then you would have to create a File::version (thumb) object, but you cannot call another file method on a thumb, file methods can only be called on Kirby\Cms\File objects (or assets), so you cannot do
$file->bw()->resize()
So your watermark()
file method has to either return a proper File object, or you would have to do the resizing inside your watermark()
method if that’s possible.
Thank you, I already did the second way. If somebody needs that, I’m sharing the code:
<?php
require_once __DIR__ . '/vendor/claviska/SimpleImage.php';
use Kirby\Cms\App as Kirby;
use claviska\SimpleImage;
use Kirby\Cms\File;
use Kirby\Image\Image as Image;
Kirby::plugin("texnixe/watermark", [
'fileMethods' => [
'watermark' => function($overlay = NULL) {
return site()->watermark($this, $overlay);
},
],
'siteMethods' => [
'watermark' => function($image, $overlay = NULL) {
if ($overlay === NULL)
$overlay = url(kirby()->url('media') . '/plugins/texnixe/watermark/img/watermark-medium.png');
$filename = $image->name() . '-watermark.' . $image->extension();
$root = kirby()->root('media');
$base = '/plugins/texnixe/cache/';
if (!empty(trim($image->parent()->uri(), '/')))
$base .= trim($image->parent()->uri(), '/') . '/';
else if (!empty($image->id()))
$base .= trim(F::dirname($image->id()), '/') . '/';
$path = $root . $base . $filename;
if (!F::exists($path)) {
$img = new SimpleImage($image->root());
$img = $img->overlay($overlay, 'center center', 1, -0, -0); // change settings as needed
if (!Dir::exists($root.$base))
Dir::make($root.$base);
$img->toFile($path);
}
return new Image([ 'url' => kirby()->url('media') . $base . $filename, 'root' => $image->root() ]);
// return new File([ 'alt' => 'test', 'filename' => $filename, 'parent' => $image->parent(), 'root' => $path, 'url' => kirby()->url('content') . $base . $filename, 'id' => 0/*$image->id()*/ ]);
},
],
];
Usage:
//simple
echo '<img src="' . site()->watermark($page->image())->url() . '" />';
echo '<img src="' . $page->image()->watermark()->url() . '" />';
echo $page->image()->watermark()->html();
//with resize
echo '<img src="' . site()->watermark($page->image()->resize(800,1200, 90))->url() . '" />';
echo site()->watermark($page->image()->resize(800,1200, 90))->html();
//custom watermark
echo '<img src="' . $page->image()->watermark(url(kirby()->url('media') . '/plugins/texnixe/watermark/img/watermark2.png'))->url() . '" />';
echo '<img src="' . site()->watermark($page->image(), url(kirby()->url('media') . '/plugins/texnixe/watermark/img/watermark2.png'))->url() . '" />';