Add file to page with plugin

I want to add a static image file to a page programmatically. I have tried using the onPageProcessed and onPageContentRaw events.

Using any of the events, I am able to save the file to the folder using standard PHP functions.

Unfortunately, I need to clear the cache to be able to see the image in my site (it is added to the page through the theme, like the header image in quark).

So I need to automatically clear/invalidate the page cache or add the image nicely through the Grav API (i think).

How should I do this?