Hello,
I have a question that’s probably asked several times, but I don’t find the right keywords for my search.
I created a hook that converts an uploaded HEIC image into a JPG using Imagick. But in my panel, still the heic image is present and not the jpg - I manually have to go to “Select”, deselect the heic and select the jpg.
How can I swap the two files automatically within the hook? Or do I have a wrong understanding how to accomplish this?
Here’s my code so far:
return [
'hooks' => [
'file.create:after' => function (Kirby\Cms\File $file) {
$ext = strtolower($file->extension());
if($ext == 'heic' || $ext == 'heif')
{
$source = $file->root();
$targetName = $file->name() . '.jpg';
$targetPath = $file->parent()->root() . '/' . $targetName;
$img = new Imagick($source);
$img->autoOrient();
// $img->resizeImage(2048, 2048, Imagick::FILTER_LANCZOS, 1, true);
$img->setImageFormat('jpeg');
$img->setImageCompressionQuality(90);
$img->writeImage($targetPath);
$img->clear();
$img->destroy();
// unlink($source);
// swap images in gallery:
// *todo*
}
}
]
]
Thanks and all the best!