How to sync image template data with a mysql database?

Hello, in order to make a system to search through a lot of photo’s. I think that’s a good idea when a file template for a photo is updated it writes to the mysql database, how can i do this? Where to start? Below the layout of the fields.

- width: 3/4
 sections:
   meta:
     type: fields
     fields:
       image_title:
         label: Image Title
         type: text
       image_description:
         label: Image description
         type: textarea
         size: small
       subject:
         label: Subject
         type: text
       person:
         label: Person
         type: text
       tags:
         label: Tags
         type: text
       photographer:
         label: Creator
         type: text
       location:
         label: Location
         type: text
       technique:
         label: Technique
         type: text


- width: 1/4
 sections:
   content:
     type: fields
     fields:
       id_link_album:
         label: Album
         type: text
         width: 1/3
       date_created:
         label: Created Date
         type: date
         time: false
         width: 2/3
       date_acquisition:
         label: Acquisition Date
         type: date
         width: 2/3```

mysql tabel kirby_images

"id","id_image","id_link_album","slug","title","status","image_title","image_description","subject","photographer","person","file","name","template","sort","content","tags","location","technique","date_created","date_acquisition"

You can do that in a file.update:after hook.

Thank you, i came a bit further!

In my page.update:after hook i want to check if the files are in the database or else add them to the database, with this code only the first image is added, not the rest.

  'page.update:after' => function ($newPage, $oldPage) {
        foreach ($newPage->files() as $file) {
            $file->changeName(F::safeName($file->name()))->update([
                'template' => $file->type()
            ]);
            if (Db::first('kirby_images', '*', [
                'id_file' => $file->id(),
                'slug' => $file->name()
            ])) {

                Db::insert('kirby_images', [
                    'id_file' => $file->id(),
                    'slug' => $file->name()
                ]);
            }

        }

    },
    'file.create:before' => function ($file, $upload) {
        $filename = $upload->filename();
        $filename = substr($filename, strpos($filename, ".") + 1);
        kirby()->session()->set('original_filename', $filename);
    },
    'file.create:after' => function ($file) {
        $filename = kirby()->session()->get('original_filename');
        $file->update([
            'original_filename' => $filename,
            'template' => $file->type(),
        ]);
        if (Db::first('kirby_images', '*', [
            'id_file' => $file->id(),
            'slug' => $file->name()
        ])) {

        Db::insert('kirby_images', [
            'id_file' => $file->id(),
            'slug' => $file->name()
            ]);
        }



    },

    'file.update:after' => function ($newFile, $oldFile) {

        $data = [
            'title' => $newFile->image_title(),
            'description' => $newFile->image_description(),
            'subject' => $newFile->subject(),
            'person' => $newFile->person(),
            'tags' => $newFile->tags(),
            'creator' => $newFile->creator(),
            'location' => $newFile->location(),
            'technique' => $newFile->technique(),
            'id_file' => $newFile->id(),
            'slug' => $newFile->name(),

        ];
        if (Db::first('kirby_images', '*', ['slug' => $oldFile->name()])) {
            Db::update('kirby_images', $data, ['slug' => $newFile->name()]);

        } else Db::insert('kirby_images', $data, ['slug' => $newFile->name()]);

    },

    'file.delete:after' => function ($status, $file) {
        Db::delete('kirby_images', ['slug' => $file->name()]);
    },

If you want to store the changed name in the database, you have to store the changed object in a (new) variable first:

 $file = $file->changeName(F::safeName($file->name()))->update([
                'template' => $file->type()
            ]);

Good morning! Yes, i would like to store the original name in the database.

    'page.update:after' => function ($newPage, $oldPage) {
        foreach ($newPage->files() as $file) {
                $oldFile = $file;
                $file->changeName(F::safeName($file->name() . '-large'))->update([
                'original_filename' => $oldFile->filename(),
                'template' => $file->type()
                ]);

        }
    },

I’ve got an older problem now…

(F::safeName($file->name())


Gives me the error:

Exception: Kirby\Exception\LogicException

The new file exists and cannot be overwritten

I could only solve this by adding something extra to the name

(F::safeName($file->name() . '-large'))

but after multiple updates i would get filenames like could-be-poisonous-large-large-large.jpg

(F::safeName($file))

with this would get could-be-poisonous.jpg.jpg.jpg