Automatic image caching

Hi, is it possible to enable automatic caching of all images referenced in markdown and twig files into the /images folder? As far as I see, by default that happens only for images which are run through a filter.

I’m using static file cache so potential overheads aren’t an issue. Ideally I’d like to have all of my images cached in the /images folder (thereby exposing nothing at all from the /user folder), but I’m pretty sure that’s impossible for images referenced in css. Would there be any other such exceptions?

It’s not possible for images referenced by css, but it is possible for all page media images. We actually made a performance optimization a while back that left images that were not manipulated as-is and referenced them where they sit in their respective page folders. This ensures that if you are not doing anything with the image, they are not processed at all, as potentially you could lose some image opitmizations, not to mention, flattening animated GIFs etc. So this is not something we want to remove.

However, I think that it could be valuable to have a configuration option, and/or an optional parameter that effectively does the caching. Such as:


This would simply process the image and save the cached version, but not perform any other operation on it. Would that suffice?

Yes, your example together with a config option to make it automatic when toggled would suffice.

As a quick and dirty solution I have attempted to reformat the markdown of each page with a plugin so that for example ![foo](bar.png) gets replaced with ![foo](bar.png?brightness=0), thereby forcing caching.

I have done this within the onPageContentRaw hook as I understood that it fires before markdown gets processed, but alas it didn’t work. Any suggestions?

Ignore my last post, I’ve found the problem - forgot to call $page->setRawContent() at the end of the hook

BTW, if you check in the develop branch of Grav on github you will see i’ve already added this system option and also the ?cache option :slight_smile:

Thanks a bunch!