Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Clarification about image caching

Solved by pamtbaau View solution

Started by Marco Cevoli 5 years ago · 6 replies · 905 views
5 years ago

Hi,

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.media.images[page.header.featured_image].cache.url() : page.media.images|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.
Marco

5 years ago Solution

@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:

YAML
// system.yaml

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

<img src="{{ page.media.images|first.url() }}">
<img src="{{ page.media.images|first.cache.url() }}">
HTML
// HTML

// 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
5 years ago

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

5 years ago

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...

5 years ago

@marcocevoli,

Yes, it's contradictory, but the HTML code is inline with my expectation. It shows a path from cache:
image|690x109

👍 1
5 years ago

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.

11 months ago

Hi marcocevoli,

Would you give me a hint how I could prepare my images for a grav website? Nowadays I use the internal system from grav to give me the correct responsive images (generated for each media query) and I use the cache system as well … however I would like to work with beautiful url & filenames as well to get my SEO better. Would a plugin like this ( https://getgrav.org/blog/turbo-charge-grav-with-a-cdn ) could help me out?

Kind regards, hope I will receive an answer from someone as this thread is already a bit old.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 80 9 hours ago
General · by pamtbaau, 14 hours ago
1 51 13 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago