-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.php
executable file
·163 lines (153 loc) · 5.11 KB
/
config.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Cms\FileVersion;
use Kirby\Text\KirbyTag;
Kirby::plugin(
'rokka/kirby',
[
'options' => [
'enabled' => false,
'organization' => 'YOUR_ORG',
'apikey' => 'YOUR_API_KEY',
],
'routes' => function ($kirby) {
return [
[
'pattern' => '_rokka/create-stacks',
'action' => function () use ($kirby) {
return Rokka::createStacks($kirby);
},
],
];
},
'fileMethods' => [
'rokkaCropUrl' => function ($width, $height = 10000, $format = null) {
return Rokka::getCropUrl($this, $width, $height, $format);
},
'rokkaResizeUrl' => function ($width, $height = 10000, $format = null) {
return Rokka::getResizeUrl($this, $width, $height, $format);
},
'rokkaOriginalSizeUrl' => function ($format = null) {
return Rokka::getOriginalSizeUrl($this, $format);
},
'rokkaGetHash' => function () {
return Rokka::getRokkaHash($this);
},
'rokka' => function ($stack, $extension = null) {
return Rokka::getImgTag($this, $stack, $extension);
},
],
'tags' => [
'imageRokka' => [
'attr' => [
'stack',
'format',
//from here on, these are the default attr from kirby/config/tags.php for images
'alt',
'caption',
'class',
'height',
'imgclass',
'link',
'linkclass',
'rel',
'target',
'text',
'title',
'width'
],
'html' => function (KirbyTag $tag) {
//Fallback to original kirby image kirby tag, if rokka is not enabled
if (!Rokka::isEnabled()) {
return Rokka::getOriginalImageTag()['html']($tag);
}
$file = $tag->file($tag->attr('imageRokka'));
if ($file == null) {
if (url::isAbsolute($tag->attr('imageRokka'))) {
//use kirby image tag impl, if we have an absolute url
return Rokka::getOriginalImageTag()['html']($tag);
} else {
// don't return any image tag, if the file doesn't exist
return "";
}
}
$stacks = option('rokka.kirby.stacks');
$extension = $file->extension();
$ext = null;
if ($extension == 'svg') {
$stack = $stacks['raw'];
$ext = $extension;
} else if ($width = $tag->attr('width')) {
$options = "resize-width-$width";
if ($height = $tag->attr('height')) {
$options .= "-height-$height";
}
if (isset($stacks['resize'])) {
$stack = $stacks['resize'] . "/$options";
} else {
$stack = "dynamic/$options--options-autoformat-true";
}
} else if (isset($stacks['kirbytext'])) {
$stack = $stacks['kirbytext'];
} else if (isset($stacks['noop'])) {
$stack = $stacks['noop'];
} else {
$stack = "dynamic/options-autoformat-true";
}
$stack = $tag->attr('stack', $stack);
if (!$ext) {
$ext = $tag->attr('format', 'jpg');
}
if ($file == false) {
$file = null;
}
return Rokka::getImgTag($file, $stack, $ext, $tag);
},
],
],
'hooks' => [
'kirbytags:before' => function ($text, $data, $options) {
// replace all (image: with (imageRokka:
if (Rokka::isEnabled()) {
// regex taken from \Kirby\Text\KirbyTags::parse
return preg_replace_callback('!(?=[^\]])\(image:(.*?\))!is', function ($match) {
return '(imageRokka:' . $match[1];
}, $text ?? '');
}
return $text;
},
],
'components' => [
'file::version' => function (App $kirby, File $file, array $options) {
if (!Rokka::isEnabled() || !$file->rokkaGetHash()) {
// fallback to the default one
$components = include $kirby->root('kirby') . '/config/components.php';
return $components['file::version']($kirby, $file, $options);
}
$width = $options['width'] ?? null;
$height = $options['height'] ?? null;
$format = $options['format'] ?? $file->extension();
$format = strtolower($format);
if ($format !== 'png') {
$format = 'jpg';
}
if (isset($options['grayscale']) && $options['grayscale']) {
$url = Rokka::getGrayscaleUrl($file, $format);
} else if (isset($options['crop']) && $options['crop']) {
$url = Rokka::getCropUrl($file, $width, $height, $format);
} else if (isset($options['blur']) && $options['blur']) {
$url = Rokka::getBlurUrl($file, $options['blur'], $format);
} else {
$url = Rokka::getResizeUrl($file, $width, $height, $format);
}
return new FileVersion([
'modifications' => $options,
'original' => $file,
'root' => $file->root(),
'url' => $url,
]);
},
],
]
);