File Hook to Convert Image Type on Upload

In Kirby 2, I created a little plugin that handled file uploads/replacements by: resizing them and converting them to JPEG. (It involved creating a thumb() version on 'panel.file.upload' and panel.file.replace' and then moving files around (including the metadata text file) and deleting the original.)

I’m trying figure out how to adapt this in the new Kirby 3 hook system, and not really sure what the best approach would be.

It would seem to make the most sense to do this in the file.create:before hook, but it doesn’t seem like I can actually modify the Kirby\Cms\File object to actually make that happen.

If I use file.create:after, I suppose I could immediately delete the uploaded file and then “create” the new file. But this seems very hacky and the wrong approach.

Any suggestions on how to approach this? Or, do you know of a plugin that already does this?

The only one I’m aware of is this that does resizing on upload without file conversion: https://github.com/medienbaecker/kirby-autoresize, using a file.upload:after hook.

Thanks for the suggestion. The lack of file conversion was definitely a dealbreaker, but I ended up making my own plugin that does this. (I also didn’t want the resizing/conversion to happen on every upload.)

When I get a chance, I’ll try to clean it up a bit and publish it as a proper plugin.

1 Like