-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.php
43 lines (37 loc) Β· 1.1 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
@include_once __DIR__ . '/vendor/autoload.php';
@include_once __DIR__ . '/src/webp.php';
function shouldGenerateWebP($file) {
return $file->kirby()->option('kirby3-webp', false);
}
function generateWebP($file) {
(new WebP\Convert)->generateWebP($file);
}
function deleteWebPFiles($file) {
$webpFile = dirname($file->root()) . '/' . $file->name() . '.webp';
$webpTxtFile = dirname($file->root()) . '/' . $file->name() . '.webp.txt';
deleteIfExist($webpFile);
deleteIfExist($webpTxtFile);
}
function deleteIfExist($file) {
if (F::exists($file)) {
F::remove($file);
}
}
Kirby::plugin('felixhaeberle/kirby3-webp', [
'hooks' => [
'file.create:after' => function ($file) {
if (shouldGenerateWebP($file)) {
generateWebP($file);
}
},
'file.replace:after' => function ($newFile, $oldFile) {
if (shouldGenerateWebP($newFile)) {
generateWebP($newFile);
}
},
'file.delete:after' => function ($file) {
deleteWebPFiles($file);
},
],
]);