Auto-scale images on upload based on orientation

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?

Please close all braces correctly, there’s one missing after the second if and the function itself is not closed either. Correct indentation helps to identify such issues…

Thanks again @texnixe. Apologies, I should have looked over my code more clearly. I had the error set inside the braces of the try function which was breaking it. Also used the $file->isPortrait() and $file->isLandscape() functions instead of the orientation ones and it seems to be working!

Here is the finished code for anyone else who may have a similar requirement:

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->isPortrait() 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->isLandscape() 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, $maxHeight);
            $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());
    }
};