Transferring Metadata on image resize when a hook is used to resize on upload

Hi there,

I’m trying to resize images on upload via the panel, but I want to maintain the metadata stored in the image. I’ve gone about this using a hook and custom plugin based off another post in the forum and it resizes the image but doesn’t maintain the metadata and I just can’t quite figure out what I’m doing wrong so any advice would be appreciated.

site/config/config.php

  'hooks' => [
        'file.create:after' => function ($file) {
          // Orginal file root location
          $orginalFileLocation = $file->root();

          // Create Resized Image
          $thumbnail = $file->thumb(['width' => 2048]);

          // Copy EXIF Data to Thumbnail
          $file->transferIptcExif2File($thumbnail);

          // Overwrite Orginal File
          $thumbnail->move($orginalFileLocation, true);
        }
    ],

site/plugins/thumbs/index.php

<?php

  // Here I’ve inserted the transferIptcExif2File function from https://www.php.net/manual/en/function.iptcembed.php#113877

  Kirby::plugin('exif/plugin', [
    'fileMethods' => [
      'transferIptcExif2File' => function($original, $thumb) {
        if(!$thumb->exif() || empty($thumb->exif()->data())) {
          if ($original->exif() && !empty($original->exif()->data())) {
            transferIptcExif2File($original->root(), $thumb->root());
          }
        }
        return $thumb;
      }
    ]
  ]);
?>

Your method expects two parameters while you are only passing one. Since it is a file method, you have access to the original via $this inside the method, so remove the $original parameter

Ah silly mistake my side. I’ve given that a go and it still doesn’t seem to be copying the EXIF data across to the thumbnail for some reason. I’m wondering if the ->move part is doing what I think it’s doing.

site/config/config.php

  'hooks' => [
      'file.create:after' => function ($file) {
        // Orginal file root location
        $orginalFileLocation = $file->root();

        // Create Resized Image
        $thumbnail = $file->thumb(['width' => 2048]);

        // Copy EXIF Data to Thumbnail
        $file->transferIptcExif2File($thumbnail);

        // Overwrite Orginal File
        $thumbnail->move($orginalFileLocation, true);
      }
    ],

site/plugins/thumbs/index.php

<?php

  // Here I’ve inserted the transferIptcExif2File function from https://www.php.net/manual/en/function.iptcembed.php#113877

  Kirby::plugin('exif/plugin', [
    'fileMethods' => [
      'transferIptcExif2File' => function($thumb) {
        if(!$thumb->exif() || empty($thumb->exif()->data())) {
          if ($this->exif() && !empty($this->exif()->data())) {
            transferIptcExif2File($this->root(), $thumb->root());
          }
        }
        return $thumb;
      }
    ]
  ]);
?>

Does $thumbnail->move() do anything, i.e. move the file where it is supposed to be?

And also, while your file method manipulates the thumb, you do not store the result in a new variable, so if anything, you move the original thumb.

$thumbnail = $file->transferIptcExif2File($thumbnail);