Thumbnail on upload - Keep the original and the generated image

330/5000

Hello

I’m trying to make a Hook to resize images on upload in the panel as in the example of Kirby doc :

Auto-resize images on upload

<?php
kirby()->hook('panel.file.upload', 'resizeImage');
kirby()->hook('panel.file.replace', 'resizeImage');

function resizeImage($file) {
  // set a max. dimension
  $maxDimension = 1000;
  try {
    // check file type and dimensions
    if ($file->type() == 'image' and ($file->width() > $maxDimension or $file->height() > $maxDimension)) {

      // get the original file path
      $originalPath = $file->dir() . '/' . $file->filename();
      // create a thumb and get its path
      $resizedImage = $file->resize($maxDimension, $maxDimension);
      $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());
  }
}

But how to adapt this code to keep the two images (the original and the smaller version) with each one a filename to disting them like :

  • image1.jpg
  • small-image1.jpg

Thanks for your help