Hey Kirby users,
I want to auto-scale images that the user uploads and am using @bastianallgeier 's method from here https://getkirby.com/docs/cookbook/handling-images-in-kirby , however that plugin works so that if either the width or the height is higher than the variable $maxDimensions then it will be rescaled based on the highest of the width or height. I want maximum width and maximum height to be a different value, so that the resizing varies depending on whether the image is landscape (max width 1820px) or portrait orientation (max height 1120px).
This is one of the methods I tried, but I seem to be breaking my page:
kirby()->hook(āpanel.file.uploadā, āresizeImageā);
kirby()->hook(āpanel.file.replaceā, āresizeImageā);
function resizeImage($file) {
// set a max. dimension
$maxWidth = 1820;
$maxHeight = 1120;
try {
if ($file->type() == 'image' and $file->dimensions()->portrait() and $file->height() > $maxHeight) {
// get the original file path
$originalPath = $file->dir() . '/' . $file->filename();
// create a thumb and get its path
$resizedImage = $file->resize($maxHeight, $maxHeight);
$resizedPath = $resizedImage->dir() . '/' . $resizedImage->filename();
// replace the original image with the resized one
copy($resizedPath, $originalPath);
unlink($resizedPath);
}
if ($file->type() == 'image' and $file->landscape() and $file->width() > $maxWidth) {
// get the original file path
$originalPath = $file->dir() . '/' . $file->filename();
// create a thumb and get its path
$resizedImage = $file->resize($maxWidth, $maxWidth);
$resizedPath = $resizedImage->dir() . '/' . $resizedImage->filename();
// replace the original image with the resized one
copy($resizedPath, $originalPath);
unlink($resizedPath);
} catch (Exception $e) {
return response::error($e->getMessage());
}
Any suggestions?