Clarification about image caching


I don’t understand how image caching works in Grav.

We usually upload a few images for each blog post, including a cover image. We don’t use image actions on them, just the occasional lightbox and some classes.

The images we upload are optimized in size. I expect that Grav doesn’t cache images that are not manipulated.

However, if I look at the actual generated URL for the cover images, they are indeed cached and resized and I don’t understand why or where.

The part of the template controlling this is:
{% set featured_image_url = page.header.featured_image ?[page.header.featured_image].cache.url() :|first.cache.url() %}

This is my Configuration > System:

I’d like Grav to respect our image size and optimization, serving the exact images we upload. How can you do that?

Thanks in advance.

@marcocevoli, The docs do indeed seem to be confusing…

In my interpretation, the function image.cache should do what its name implies: Use the cache.

Let’s do a test:

// system.yaml

  default_image_quality: 85
  cache_all: false
// default.html.twig

<img src="{{|first.url() }}">
<img src="{{|first.cache.url() }}">

// 57.2 K
<img src="/user/pages/01.home/windows background.jpg">

// 47.8 K
<img src="/images/7/7/4/f/6/774f676a3265d6eb3369627af6132582d538af2c-windows-background.jpg">

As you can see, the function image.cache does indeed force the image to be cached and compressed.

1 Like

Well, then the documentation needs a review, because it states the opposite:

And the compression uses whatever PHP compression method is used by Grav, which often times results in files larger than the original. I have a 25k image in the item folder that becomes 72k…


Yes, it’s contradictory, but the HTML code is inline with my expectation. It shows a path from cache:

1 Like

Sorry if I resurrect this old thread, but I found a different solution to this, because the one suggested wasn’t working on our site.

Images are manipulated by Grav if you toggle “SEO-Friendly Image names” to “Yes”. This adds a small hash after the file name, and so all images are manipulated / re-compressed by Grav, causing them to be usually much larger than already optimized images.

Leaving that option on the default value (NO) and setting “Cache all images” to No, finally has as a result that images aren’t cached nor compressed by Grav.