Skip to content

Commit 7b2e33b

Browse files
authored
Create resizeimage.snippet.php
0 parents  commit 7b2e33b

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

resizeimage.snippet.php

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
# vars
4+
$lib = $modx->getOption("lib", $scriptProperties, "Imagick");
5+
$libPath = $modx->getOption("libPath", $scriptProperties, "/Applications/MAMP/Library/bin/convert");
6+
$mode = $modx->getOption("mode", $scriptProperties, "");
7+
$type = $modx->getOption("type", $scriptProperties, "image");
8+
$input = $modx->getOption("input", $scriptProperties, "");
9+
$sizes = $modx->getOption("options", $scriptProperties, "");
10+
$quality = $modx->getOption("quality", $scriptProperties, 70);
11+
$fileExtension = $modx->getOption("fileExtension", $scriptProperties, "webp");
12+
$setRatio = $modx->getOption("setRatio", $scriptProperties, true);
13+
$cultureKey = $modx->getOption("cultureKey");
14+
$basePath = $modx->getOption("base_path");
15+
$filePath = preg_replace("/^\/?" . $cultureKey . "\//i", "", $input);
16+
$filePathLast = $filePath;
17+
$cachePath = $modx->getOption("cachePath", $scriptProperties, "assets/image-cache/");
18+
$srcsets = [];
19+
$src = [];
20+
21+
# original image not exists
22+
if (!file_exists($basePath . $filePath)) {
23+
if ($mode == "json") {
24+
return "'" . $input . "'";
25+
}
26+
return $input;
27+
}
28+
29+
# loop sizes
30+
$sizes = explode(",", $sizes);
31+
natsort($sizes);
32+
foreach (array_reverse($sizes) as $size) {
33+
list($width, $height) = explode("x", $size);
34+
$filePathInfo = pathinfo($filePath);
35+
$savePathExtension = "." . substr(md5($filePathLast), 0, 8) . "." . $width . "x" . $height . "-" . $quality . "." . $fileExtension;
36+
$savePath = $cachePath . $filePathInfo["filename"] . $savePathExtension;
37+
38+
# check if base image exists
39+
if (!file_exists($basePath . $filePathLast) || filesize($basePath . $filePathLast) < 1) {
40+
$filePathLast = $filePath;
41+
}
42+
43+
# cached image not found
44+
if (!file_exists($basePath . $savePath)) {
45+
# check if cache dir exists
46+
if (!is_dir($basePath . $cachePath)) {
47+
mkdir($basePath . $cachePath, 0755);
48+
} else {
49+
50+
# cleanup cache
51+
foreach (glob($basePath . $cachePath . "*." . $fileExtension) as $file) {
52+
if (filemtime($file) < time() - (60 * 60 * 24 * 30)) {
53+
unlink($file);
54+
}
55+
}
56+
57+
}
58+
59+
# generate image
60+
if ($lib == "cli") {
61+
62+
if ($type == "video") {
63+
64+
$cmd = $libPath . " -ss 00:00:00 -i " . $basePath . $filePathLast . " -filter:v scale=\"" . ( $width ?? "-1" ) . ":" . ( $height ?? "-1" ) . "\" " . $basePath . $savePath;
65+
66+
} else {
67+
68+
$resize = "-resize " . ( $width ?? "" ) . "x" . ( $height ?? "" );
69+
70+
if ($width && $height) {
71+
$resize = "-resize " . $width . "x" . $height . "^ -gravity center -extent " . $width . "x" . $height;
72+
}
73+
74+
$cmd = "export OMP_NUM_THREADS=1;"; // bugfixes IONOS resize large images, thanks to https://stackoverflow.com/a/69133237
75+
$cmd .= $libPath . " " . $basePath . $filePathLast . " " . $resize . " -quality " . $quality . " " . $basePath . $savePath;
76+
77+
}
78+
79+
shell_exec($cmd);
80+
81+
} else {
82+
83+
$image = new Imagick($basePath . $filePathLast);
84+
85+
if ($width && $height) {
86+
$image->cropThumbnailImage((int) $width, (int) $height);
87+
} else {
88+
$image->thumbnailImage((int) $width, (int) $height);
89+
}
90+
91+
$image->setImageCompressionQuality($quality);
92+
$image->writeImage($basePath . $savePath);
93+
94+
}
95+
96+
}
97+
98+
# get generated image
99+
if (file_exists($basePath . $savePath)) {
100+
list($width, $height) = getimagesize($basePath . $savePath);
101+
$srcsets[] = [
102+
"url" => $savePath,
103+
"width" => $width,
104+
"height" => $height
105+
];
106+
107+
# use image for next loop
108+
$filePathLast = $savePath;
109+
}
110+
}
111+
112+
# no srcsets, maybe video is converting in the background i.e.
113+
if (!count($srcsets)) {
114+
return $input;
115+
}
116+
117+
# generate srcset
118+
foreach ($srcsets as $srcset) {
119+
$src[] = $srcset["url"] . " " . $srcset["width"] ."w";
120+
}
121+
122+
# set width and height ratio
123+
$width = $srcsets[0]["width"];
124+
$height = $srcsets[0]["height"];
125+
126+
# output as json
127+
if ($mode == "json") {
128+
return "{
129+
url: '" . $input . "',
130+
src: '" . implode(",", $src) . "',
131+
width: " . ( $width ?? 0 ) . ",
132+
height: " . ( $height ?? 0 ) . "
133+
}";
134+
}
135+
136+
# output as base64
137+
$srcs = implode(",", $src);
138+
if ($mode == "base64") {
139+
$srcs = "data:image/" . $fileExtension . ";base64," . base64_encode(file_get_contents($srcsets[0]["url"]));
140+
}
141+
142+
$o = '="' . $srcs . '" width="' . $width . '" height="' . $height . '"';
143+
144+
if ($setRatio) {
145+
$o .= ' style="aspect-ratio: ' . $width . '/' . $height . '"';
146+
}
147+
148+
return $o;

0 commit comments

Comments
 (0)