# Programmatically generate image file in media folder after upload

**URL:** <https://forum.getkirby.com/t/programmatically-generate-image-file-in-media-folder-after-upload/24555>\
**Category:** Questions\
**Tags:** v3\
**Created:** [January 25, 2022, 9:37am UTC](https://forum.getkirby.com/t/programmatically-generate-image-file-in-media-folder-after-upload/24555 "2022-01-25T09:37:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kruska](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kruska](https://forum.getkirby.com/u/kruska)\
**Post date:** [January 25, 2022, 9:37am UTC](https://forum.getkirby.com/t/programmatically-generate-image-file-in-media-folder-after-upload/24555/1 "2022-01-25T09:37:34Z")

</div>

I need to automatically generate images in the media folder after they have been uploaded. I’ve noticed that after the image is being uploaded only a thumbnail version is created in the media folder. I’ve tried wiring up a hook in config.php like this:

```auto
'hooks' => [
      'file.create:after' => function ($newfile) {
          $newfile->publish();
      }
    ]

```

The hook seems to work but the $newfile seems to be null. I am receving following error in the kirby panel:

`Call to a member function publish() on null`

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 25, 2022, 9:43am UTC](https://forum.getkirby.com/t/programmatically-generate-image-file-in-media-folder-after-upload/24555/2 "2022-01-25T09:43:01Z")

</div>

> [@kruska](#):
>
> ```auto
> 'hooks' => [
> 'file.create:after' => function ($newfile) {
> $newfile->publish();
> }
> ]
> 
> ```

The parameter has to be `$file` not `$newfile`.

> **[file.create:after](https://getkirby.com/docs/reference/plugins/hooks/file-create-after)**
>
> Reference page for the hook file.create:after

---

<div class="post-metadata">

**Author:** ![kruska](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kruska](https://forum.getkirby.com/u/kruska)\
**Post date:** [January 25, 2022, 9:45am UTC](https://forum.getkirby.com/t/programmatically-generate-image-file-in-media-folder-after-upload/24555/3 "2022-01-25T09:45:15Z")

</div>

Works! I accidentally took the $newFile from the file.update.after hook. Thanks!!! Works like charm!

---

<div class="post-metadata">

**Author:** ![jorisvdz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jorisvdz/32/7570_2.png) [@jorisvdz](https://forum.getkirby.com/u/jorisvdz)\
**Post date:** [February 17, 2022, 6:15pm UTC](https://forum.getkirby.com/t/programmatically-generate-image-file-in-media-folder-after-upload/24555/4 "2022-02-17T18:15:11Z")

</div>

So, just to be sure the code needs to be this?

```auto
 'hooks' => [
    'file.create:after' => function ($file) {
      $file->publish();
    },
  ]

```

If I use this, the media is not being generated in the media folder…

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 17, 2022, 6:27pm UTC](https://forum.getkirby.com/t/programmatically-generate-image-file-in-media-folder-after-upload/24555/5 "2022-02-17T18:27:59Z")

</div>

Code is fine and files should be published in the media folder.

---

<div class="post-metadata">

**Author:** ![jorisvdz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jorisvdz/32/7570_2.png) [@jorisvdz](https://forum.getkirby.com/u/jorisvdz)\
**Post date:** [February 17, 2022, 7:18pm UTC](https://forum.getkirby.com/t/programmatically-generate-image-file-in-media-folder-after-upload/24555/6 "2022-02-17T19:18:43Z")

</div>

Hm, than I maybe need to do some debugging. Thanks.
