# Replace image after converting it in a Hook

**URL:** <https://forum.getkirby.com/t/replace-image-after-converting-it-in-a-hook/35401>\
**Category:** Questions\
**Tags:** v4\
**Created:** [May 15, 2026, 12:34pm UTC](https://forum.getkirby.com/t/replace-image-after-converting-it-in-a-hook/35401 "2026-05-15T12:34:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![clockdiv](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@clockdiv](https://forum.getkirby.com/u/clockdiv)\
**Post date:** [May 15, 2026, 12:34pm UTC](https://forum.getkirby.com/t/replace-image-after-converting-it-in-a-hook/35401/1 "2026-05-15T12:34:13Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [May 15, 2026, 10:25pm UTC](https://forum.getkirby.com/t/replace-image-after-converting-it-in-a-hook/35401/2 "2026-05-15T22:25:07Z")

</div>

Welcome to the community 🙂

You can possible do it with `update()` to set a new value for the field. You can also deelte the heic file as well.

> **[$page-\>update()](https://getkirby.com/docs/reference/objects/cms/page/update)**
>
> Updates the page data

> **[$file-\>delete()](https://getkirby.com/docs/reference/objects/cms/file/delete)**
>
> Deletes the file. The store is used to manipulate the filesystem or whatever you prefer.

I think the best way is actually to register the heic format and then you can use kirby image manipulation without jumping throught hoops in a hook to do it.

> **[Image processing - File types](https://getkirby.com/docs/reference/plugins/extensions/file-types#image-processing)**
>
> Kirby categorizes file types by extension and MIME type. We support a wide range of file types, but of course we cannot predefine them all. You can add your own file types to the system.

That way you can use thumb() in your templates and snippets and set the format option to jpeg

> **[Examples - $file-\>thumb()](https://getkirby.com/docs/reference/objects/cms/file/thumb#examples)**
>
> Creates a modified version of images The media manager takes care of generating those modified versions and putting them in the right place. This is normally the /media folder of your installation, but could potentially also be a CDN or any other...

---

<div class="post-metadata">

**Author:** ![clockdiv](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@clockdiv](https://forum.getkirby.com/u/clockdiv)\
**Post date:** [May 16, 2026, 2:42pm UTC](https://forum.getkirby.com/t/replace-image-after-converting-it-in-a-hook/35401/3 "2026-05-16T14:42:17Z")

</div>

Thank you very much for your answer and the warm welcome!

I now tried with a basic plugin to register the file type and also played with thumbnails and the update-function.

Unfortunately, I didn’t get it to work. I think I just keep taking care that - when uploading images from iPhone directly in the Panel - I set the options in iOS to ‘most compatible’.

For a really nice user experience, the images should be converted before uploading, I guess?
