Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

El_Matella's avatar

Caching Images

Hi everybody,

I have a question about browser caching images. When I go on the network tab of the developper tools on Chrome, I can see that some (all) of my images or returning a 304 not modified 'Status Text'.

All of these images are generated by the Intervention\Image class, and I was wondering if there was a way to add a line somewhere in order to make these images "browser caching", and return a 200 code, in order for the browser to avoid making an http request?

Or maybe it's a bad practice because it stores too much files on the client side?

Thank you very much for your tips and advices!

0 likes
7 replies
simik's avatar

Intervention/image has a sister package imagecache. I haven't used it myself, but it does have a lifetime parameter, maybe it will help.

1 like
El_Matella's avatar

Hi, thank you for responding! Sadly, I think that this library doesn't do what I want. For what I understand, it caches the Intervent/Image instructions, but doesn't allow to allow browsers cache. Tell me if I'm wrong..

simik's avatar

Maybe Response's setTtl($lifetime_in_seconds) method will work. It's inherited from Symfony's base Symfony\Component\HttpFoundation\Response class. It should set proper Expires/max-age HTTP headers on the response.

pmall's avatar

It is just a matter of putting the right headers in the image response. Something like Expires/max-age as mentioned above.

El_Matella's avatar

Thank you very much for your answers. Does it mean that I should create a kind of PHP file for displaying image, and using this function in here?

simik's avatar
simik
Best Answer
Level 2

Oh, if your images are generated once and stored somewhere in public directory, then you should actually change your webserver's settings so it sets proper Expires headers on images (and maybe other static files), no need for any PHP code.

Please or to participate in this conversation.