-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresizeimage.snippet.php
165 lines (131 loc) · 5.35 KB
/
resizeimage.snippet.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
<?php
# vars
$lib = $modx->getOption("lib", $scriptProperties, "Imagick");
$libPath = $modx->getOption("libPath", $scriptProperties, "/Applications/MAMP/Library/bin/convert");
$mode = $modx->getOption("mode", $scriptProperties, "");
$type = $modx->getOption("type", $scriptProperties, "image");
$input = $modx->getOption("input", $scriptProperties, "");
$sizes = $modx->getOption("options", $scriptProperties, "");
$quality = $modx->getOption("quality", $scriptProperties, 70);
$fileExtension = $modx->getOption("fileExtension", $scriptProperties, "webp");
$setRatio = $modx->getOption("setRatio", $scriptProperties, true);
$cachePath = $modx->getOption("cachePath", $scriptProperties, "assets/image-cache/");
$cultureKey = $modx->getOption("cultureKey");
$basePath = $modx->getOption("base_path");
$filePath = preg_replace("/^\/?" . $cultureKey . "\//i", "", $input);
$filePathLast = $filePath;
$filePatheIsRemote = str_starts_with($filePath, 'https');
$srcsets = [];
$src = [];
# if svg return
if (mime_content_type($basePath . $filePath) === "image/svg+xml") {
return '="' . $input . '" width="1" height="1"';
}
# if remote image
if ($filePatheIsRemote) {
$basePath = '';
}
# original image not exists
if (empty($filePath) || ( !file_exists($basePath . $filePath) && !$filePatheIsRemote )) {
if ($mode == "json") {
return "'" . $input . "'";
}
if (!empty($input)) {
return '="' . $input . '"';
}
return false;
}
# loop sizes
$sizes = explode(",", $sizes);
natsort($sizes);
foreach (array_reverse($sizes) as $size) {
$dimensions = explode("x", $size);
$width = isset($dimensions[0]) ? $dimensions[0] : "";
$height = isset($dimensions[1]) ? $dimensions[1] : "";
$filePathInfo = pathinfo($filePath);
$savePathFilename = $modx->filterPathSegment($filePathInfo["filename"]);
$savePathExtension = "." . substr(md5($filePathLast), 0, 8) . "." . $width . "x" . $height . "-" . $quality . "." . $fileExtension;
$savePath = $cachePath . $savePathFilename . $savePathExtension;
# check if base image exists
if (!file_exists($basePath . $filePathLast) || filesize($basePath . $filePathLast) < 1) {
$filePathLast = $filePath;
}
# cached image not found
if (!file_exists($basePath . $savePath)) {
# check if cache dir exists
if (!is_dir($basePath . $cachePath)) {
mkdir($basePath . $cachePath, 0755);
} else {
# cleanup cache
foreach (glob($basePath . $cachePath . "*." . $fileExtension) as $file) {
if (filemtime($file) < time() - (60 * 60 * 24 * 30)) {
unlink($file);
}
}
}
# generate image
if ($lib == "cli") {
if ($type == "video") {
$cmd = $libPath . " -ss 00:00:00 -i '" . $basePath . $filePathLast . "' -filter:v scale=\"" . ( $width ?? "-1" ) . ":" . ( $height ?? "-1" ) . "\" " . $basePath . $savePath;
} else {
$resize = "-resize " . ( $width ?? "" ) . "x" . ( $height ?? "" );
if ($width && $height) {
$resize = "-resize " . $width . "x" . $height . "^ -gravity center -extent " . $width . "x" . $height;
}
$cmd = "export OMP_NUM_THREADS=1;"; // bugfixes IONOS resize large images, thanks to https://stackoverflow.com/a/69133237
$cmd .= $libPath . " '" . $basePath . $filePathLast . "' " . $resize . " -quality " . $quality . " '" . $basePath . $savePath . "'";
}
shell_exec($cmd);
} else {
$image = new Imagick($basePath . $filePathLast);
if ($width && $height) {
$image->cropThumbnailImage((int) $width, (int) $height);
} else {
$image->thumbnailImage((int) $width, (int) $height);
}
$image->setImageCompressionQuality($quality);
$image->writeImage($basePath . $savePath);
}
}
# get generated image
if (file_exists($basePath . $savePath)) {
list($width, $height) = getimagesize($basePath . $savePath);
$srcsets[] = [
"url" => $savePath,
"width" => $width,
"height" => $height
];
# use image for next loop
$filePathLast = $savePath;
}
}
# no srcsets, maybe video is converting in the background i.e.
if (!count($srcsets)) {
return $input;
}
# generate srcset
foreach ($srcsets as $srcset) {
$src[] = $srcset["url"] . " " . $srcset["width"] ."w";
}
# set width and height ratio
$width = $srcsets[0]["width"];
$height = $srcsets[0]["height"];
# output as json
if ($mode == "json") {
return json_encode([
"url" => $input,
"src" => implode(",", $src),
"width" => $width ?? 0,
"height" => $height ?? 0
]);
}
# output as base64
$srcs = implode(",", $src);
if ($mode == "base64") {
$srcs = "data:image/" . $fileExtension . ";base64," . base64_encode(file_get_contents($srcsets[0]["url"]));
}
$o = '="' . $srcs . '" width="' . $width . '" height="' . $height . '"';
if ($setRatio) {
$o .= ' style="aspect-ratio: ' . $width . '/' . $height . '"';
}
return $o;