/
functions.php
93 lines (74 loc) · 2.48 KB
/
functions.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
<?php
require_once('vendor/autoload.php');
function clearRelatives(string $in) : string {
return str_replace(['unix:', '://', '../'], '', $in);
}
function clearQueryString(string $in) : string {
return str_replace(['/','#','$','!','%','&'], '', $in);
}
function resolvePathPrefix(string $incomingPath, string $baseDir, ?string $rootDir = null) : string {
if(!$rootDir) {
$rootDir = $baseDir;
}
$incomingPath = clearRelatives($incomingPath);
if(substr($incomingPath, 0, strlen($baseDir)) === $baseDir) {
$incomingPath = substr($incomingPath, strlen($baseDir));
}
return str_finish($rootDir, '/') . clearRelatives($incomingPath);
}
function parseOptions($queryString) : array {
$opts = explode(',', $queryString);
$options = [
'mode' => 'ORIGINAL',
'width' => 100,
'height' => 100,
'x' => 0,
'y' => 0,
'extension' => 'jpg',
'quality' => 90,
];
if(sizeof($opts) <= 0) {
return $options;
}
foreach($opts as $opt) {
if(strlen(trim($opt)) <= 0) continue;
$dotPos = strpos($opt, '.');
if($dotPos !== false) { // strip extension from the last option
$options['extension'] = substr($opt, $dotPos + 1);
$opt = substr($opt, 0, $dotPos);
}
if(strlen(trim($opt)) <= 0) {
continue;
}
switch(strtolower($opt[0])) {
case 'w': $options['width'] = intval(substr($opt, 1)); break;
case 'h': $options['height'] = intval(substr($opt, 1)); break;
case 'x': $options['x'] = intval(substr($opt, 1)); break;
case 'y': $options['y'] = intval(substr($opt, 1)); break;
case 'm': $options['mode'] = strtoupper(substr($opt, 1)); break;
case 'q': $options['quality'] = intval(substr($opt, 1)); break;
}
}
return $options;
}
function debug(string $message, bool $debugMode) : void {
if (!$debugMode) {
return;
}
error_log("\n<" . date('Y-m-d H:i:s') . '> ' . $message, 3, str_finish(__DIR__, '/') . 'debug.log');
}
function getMimeTypeFromExtension(string $extension) : string {
switch (strtolower(trim($extension))) {
case 'jpg';
case 'jpeg':
return 'image/jpeg';
case 'png':
return 'image/png';
case 'gif':
return 'image/gif';
case 'webp':
return 'image/webp';
default:
return 'text/plain';
}
}