Save a clipped image created by a plugin to the same path as the original image and rename the created image

Hello Kirby community,

I hope I might get some help here, as I am a beginner with PHP and cannot find a solution to my problem.

I have installed the k3-image-clip plugin and am currently writing my extension of this plugin to save the clipped image to the same path as the original while renaming it to include the ending “-clipped”.

I have now been able to get the hook working. I just extended the index.php of the k3-image-clip. It now shows an error message. All the details below.

Thanks for the help.


<?php

Kirby::plugin('mullema/k3-image-clip', [
    'components' => [
        'file::version' => include __DIR__ . '/components/file-version.php',
        'thumb' => include __DIR__ . '/components/thumb.php'
    ],
    'fields' => [
        'image-clip' => include __DIR__ . '/fields/image-clip.php'
    ],
    'fieldMethods' => [
        'toImage' => include __DIR__ . '/fieldMethods/toImage.php',
        'toImages' => include __DIR__ . '/fieldMethods/toImages.php',
    ],
    'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
            $image = $newPage->content()->images();
            $oldFilename = $image->filename();

            try {
                $newFilename = $oldFilename . "-clipped";
                $image->clip()->save($newFilename, $image->root());
                $image->rename($newFilename);
                echo ($image);

            } catch (Exception $e) {
                echo 'The file has not not been created';
            }
        }
    ]
]);

Message shown on save of image is:

- id: bild-home-business.jpg clip: width: 1392 height: 1800 left: 914 top: 0

I have now changed the code to this:

'page.update:after' => function ($newPage, $oldPage) {
            if ($image = $newPage->content()->images()->toImage()) {
                $image->save($newPage->root() . '/' . $image->clip() . '-clipped.jpg');
            }
        }

Okay. I think I found a solution I am happy with. It works. I didn’t want to save seperate images, but I am updating the content of the page and adding a URL to the needed field. This is what I am going with for now.

Now I just have to change it so that, only in the case the image has a specific word in its filename, it will change the field ‘portrait’.

'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
            if ($newPage->portrait()->exists()) {
                if ($image = $newPage->content()->images()->toImage()) {
                    try {
                        $newPage->update(
                            array(
                                'portrait' => $image->clip()->url(),
                            )
                        );
                    } catch (Exception $e) {
                        echo $e->getMessage();
                    }
                }
            }
        }
    ]

I somehow fail to see the purpose of storing this value in the page content?

Hello @texnixe,

I am going for a headless system and I need this image or its URL for my other system.
Three seperate images or selection fields or URL fields, so that these three user-clipped images can then be used in the other project system via KQL.