-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
91 lines (69 loc) · 2.45 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
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
<?php
use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Cms\FileVersion;
function endsWith($haystack, $needle) {
return substr($haystack,-strlen($needle))===$needle;
}
function imgix($url, $params = [])
{
if (is_object($url) === true) {
$url = $url->url();
}
// always convert urls to path
$path = Url::path($url);
// return the plain url if imgix is deactivated
if (option('imgix', false) === false or option('imgix.domain', false) === false or endsWith($url, '.gif')) {
return $url;
}
$defaults = option('imgix.defaults', []);
$params = array_merge($defaults, $params);
$options = [];
$map = [
'width' => 'w',
'height' => 'h',
];
foreach ($params as $key => $value) {
if (isset($map[$key]) && !empty($value)) {
$options[] = $map[$key] . '=' . $value;
}
elseif (!isset($map[$key]) && !empty($value)) {
$options[] = $key . '=' . $value;
}
}
$options = implode('&', $options);
return option('imgix.domain') . $path . '?' . $options;
}
Kirby::plugin('diesdasdigital/imgix', [
'components' => [
'file::version' => function (App $kirby, File $file, array $options = []) {
static $originalComponent;
if (option('imgix', false) !== false) {
$url = imgix($file->mediaUrl(), $options);
return new FileVersion([
'modifications' => $options,
'original' => $file,
'root' => $file->root(),
'url' => $url,
]);
}
if ($originalComponent === null) {
$originalComponent = (require $kirby->root('kirby') . '/config/components.php')['file::version'];
}
return $originalComponent($kirby, $file, $options);
},
'file::url' => function (App $kirby, File $file): string {
static $originalComponent;
if (option('imgix', false) !== false) {
if ($file->type() === 'image') {
return imgix($file->mediaUrl());
}
return $file->mediaUrl();
}
if ($originalComponent === null) {
$originalComponent = (require $kirby->root('kirby') . '/config/components.php')['file::url'];
}
return $originalComponent($kirby, $file);
}
]
]);