-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
201 lines (133 loc) · 5.01 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
require_once("lib/Tinify/Exception.php");
require_once("lib/Tinify/ResultMeta.php");
require_once("lib/Tinify/Result.php");
require_once("lib/Tinify/Source.php");
require_once("lib/Tinify/Client.php");
require_once("lib/Tinify.php");
function uploadExtended($file) {
$rename = option('werbschaft.uploadExtended.rename');
$excludeCharacters = option('werbschaft.uploadExtended.excludeCharacters');
$kirbyResize = option('werbschaft.uploadExtended.kirbyResize');
$maxWidth = option('werbschaft.uploadExtended.maxWidth');
$maxHeight = option('werbschaft.uploadExtended.maxHeight');
$quality = option('werbschaft.uploadExtended.quality');
$excludeTemplates = option('werbschaft.uploadExtended.excludeTemplates');
$excludePages = option('werbschaft.uploadExtended.excludePages');
$tinyPng = option('werbschaft.uploadExtended.tinyPng');
$tinyPngKey = option('werbschaft.uploadExtended.tinyPngKey');
$tinyPngResize = option('werbschaft.uploadExtended.tinyPngResize');
$tinyPngResizeMethod = option('werbschaft.uploadExtended.tinyPngResizeMethod');
$uploadLimit = option('werbschaft.uploadExtended.uploadLimit');
$uploadLimitMegabyte = option('werbschaft.uploadExtended.uploadLimitMegabyte');
$debug = option('werbschaft.uploadExtended.debug');
$message = '';
$excluded = false;
if( !empty($excludeTemplates) || !empty($excludePages ) ) {
$excluded = in_array( $file->page()->intendedTemplate(), $excludeTemplates ) || in_array( $file->page()->uid(), $excludePages );
}
if ( $rename == true ) {
$fileName = str_replace($excludeCharacters,'-',$file->name(),$count);
if ( $count > 0 ) {
$file = $file->changeName($fileName);
$message .= ' The file was renamed according to the specifications. ';
}
}
if ( !$excluded ) {
if ( $file->isResizable() ) {
// RESIZE THE IMAGE
if ( $kirbyResize == true ) {
if( $file->width() > $maxWidth || $file->height() > $maxHeight ) {
if ( $file->width() > $maxWidth ) {
$message .= ' The file is with ' . $file->width() . ' pixels wider than the allowed ' . $maxWidth . ' pixels and was reduced accordingly. ';
}
if ( $file->height() > $maxHeight ) {
$message .= ' The file is with ' . $file->height() . ' pixels higher than the allowed ' . $maxHeight . ' pixels and was reduced accordingly. ';
}
try {
kirby()->thumb($file->root(), $file->root(), [
'width' => $maxWidth,
'height' => $maxHeight,
'quality' => $quality
]);
} catch (Exception $e) {
$message .= $e->getMessage();
}
}
}
// CHECK FOR TINY PNG
if ( $tinyPng == true ) {
try {
\Tinify\setKey($tinyPngKey);
\Tinify\validate();
$compressionsCount = \Tinify\compressionCount() + 1;
$message .= ' There have already been ' . $compressionsCount . ' compressions made with the TinyPNG API key this month. 500 are free per Key. ';
} catch(\Tinify\Exception $e) {
$message .= ' The Tiny PNG Api Key is not correct. Disable Tiny PNG or enter a valid key. ';
}
// UPLOAD AND MINIFY
$fileName = $file->name();
if ( $file->page() ) {
$path = $file->page()->root();
} else {
$path = site()->root();
}
try {
$source = \Tinify\fromFile($file->root());
// RESIZE THROUGH TINY PNG
if ( $tinyPngResize == true ) {
$source = $source->resize([
"method" => $tinyPngResizeMethod,
"width" => $maxWidth,
"height" => $maxHeight
]);
}
$source->toFile( $path . '/' . $fileName . '.' . $file->extension() );
$message .= ' The file was optimized by TinyPNG. ';
} catch(\Tinify\Exception $e) {
$message .= $e->getMessage();
}
}
} else {
if ( $uploadLimit == true ) {
$uploadLimitBytes = $uploadLimitMegabyte * 1048576;
$fileSizeBytes = $file->size();
$fileSizeNice = $file->niceSize();
if ( $fileSizeBytes > $uploadLimitBytes ) {
$file->delete();
$message .= ' The file is with ' . $fileSizeNice . ' too big for the upload and was deleted because the limit for files is ' . $uploadLimitMegabyte . ' MB. ';
}
}
}
}
if ( $debug == true ) {
throw new Exception($message);
}
}
Kirby::plugin('werbschaft/uploadExtended', [
'options' => [
'rename' => true,
'excludeCharacters' => ['_','__','___','--','---'],
'kirbyResize' => true,
'maxWidth' => 2000,
'maxHeight' => 2000,
'quality' => 100,
'debug' => false,
'tinyPng' => true,
'tinyPngKey' => "insert-here",
'tinyPngResize' => false,
'tinyPngResizeMethod' => 'thumb',
'excludeTemplates' => [],
'excludePages' => [],
'uploadLimit' => true,
'uploadLimitMegabyte' => 5
],
'hooks' => [
'file.create:after' => function ($file) {
uploadExtended($file);
},
'file.replace:after' => function ($newFile, $oldFile) {
uploadExtended($newFile);
}
]
]);