-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
110 lines (85 loc) · 2.98 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Cms\FileVersion;
function cloudinary($url, $params = [], $type = 'image')
{
if (is_object($url) === true) {
$url = $url->url();
}
// always convert urls to absolute urls
$url = url($url);
// return the plain url if cloudinary is deactivated
if (option('cloudinary', false) === false) {
return $url;
}
$url = str_replace(kirby()->url('media') . '/', '', $url);
$defaults = [
'q' => 'auto',
'f' => 'auto',
'g' => 'auto',
'c' => 'fill',
'cs' => 'srgb'
];
if ($type == 'video') {
$defaults = [
'q' => 'auto:eco',
'f' => 'mp4',
'w' => 1600
];
}
$defaults = array_merge($defaults, option('cloudinary.defaults', []));
$params = array_merge($defaults, $params);
unset($params['crop']);
$options = [];
$map = [
'width' => 'w',
'height' => 'h',
'crop' => 'c',
];
foreach ($params as $key => $value) {
if (isset($map[$key]) && !empty($value)) {
$options[] = $map[$key] . '_' . $value;
} elseif (!empty($value)) {
$options[] = $key . '_' . $value;
}
}
$options = implode(',', $options);
return "https://res.cloudinary.com/". option("cloudinary.cloudname") ."/$type/upload/". $options . "/" . option("cloudinary.folder") . "/" . $url;
}
Kirby::plugin('getkirby/cloudinary', [
'components' => [
'file::version' => function (App $kirby, File $file, array $options = []) {
static $originalComponent;
if (option('cloudinary', false) !== false) {
$url = cloudinary($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('cloudinary', false) !== false) {
if ($file->type() === 'image') {
return cloudinary($file->mediaUrl());
}
if ($file->type() === 'video' && $file->filename() != 'addpmp.mp4') {
return cloudinary($file->mediaUrl(), [], 'video');
}
return $file->mediaUrl();
}
if ($originalComponent === null) {
$originalComponent = (require $kirby->root('kirby') . '/config/components.php')['file::url'];
}
return $originalComponent($kirby, $file);
}
]
]);