Last active
October 22, 2023 11:20
-
-
Save jenswittmann/36c65e2775a1cd2cb8124fe5e5d05df6 to your computer and use it in GitHub Desktop.
MODX resizeImage Snippet (modern pthumb replacement with srcset generation and very low options, but works on IONOS too)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# vars | |
$lib = $modx->getOption("resizeImage_imageLib", null, "cli"); | |
$libPath = $modx->getOption("resizeImage_imageLibPath", null, "/Applications/MAMP/Library/bin/convert"); | |
$mode = $modx->getOption("mode", $scriptProperties, ""); | |
$input = $modx->getOption("input", $scriptProperties, ""); | |
$sizes = $modx->getOption("options", $scriptProperties, ""); | |
$quality = $modx->getOption("quality", $scriptProperties, 70); | |
$cultureKey = $modx->getOption("cultureKey"); | |
$basePath = $modx->getOption("base_path"); | |
$filePath = preg_replace("/^\/?" . $cultureKey . "\//i", "", $input); | |
$filePathLast = $filePath; | |
$cachePath = $modx->getOption("cachePath", $scriptProperties, "assets/image-cache/"); | |
$srcsets = []; | |
$src = []; | |
# original image not exists | |
if (!file_exists($basePath . $filePath)) { | |
return $input; | |
} | |
# loop sizes | |
$sizes = explode(",", $sizes); | |
natsort($sizes); | |
foreach (array_reverse($sizes) as $size) { | |
list($width, $height) = explode("x", $size); | |
$filePathInfo = pathinfo($filePath); | |
$savePathExtension = "." . substr(md5($filePathLast), 0, 8) . "." . $width . "x" . $height . "-" . $quality . ".webp"; | |
$savePath = $cachePath . $filePathInfo["filename"] . $savePathExtension; | |
# check if base image exists | |
if (!file_exists($basePath . $filePathLast) || filesize($basePath . $filePathLast) < 1) { | |
$filePathLast = $filePath; | |
} | |
# image already saved | |
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 . "*.webp") as $file) { | |
if (filemtime($file) < time() - (60 * 60 * 24 * 30)) { | |
unlink($file); | |
} | |
} | |
} | |
# generate image on IONOS via cli | |
if ($lib == "cli") { | |
$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; | |
} | |
} | |
# 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, | |
"height" => $height | |
]); | |
} | |
return '="' . implode(",", $src) . '" width="' . $width . '" height="' . $height . '" style="aspect-ratio: ' . $width . '/' . $height . '"'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment