Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headers for PNG image output to make sure it gets cached at browser?

Tags:

I have images (PNG) that are generated dynamically and will be embedded in websites and forums. When an image gets posted on a very busy page, there are a lot many connections to service for something that doesn't change often. I want to tell the browser for how long to cache it.

So what headers do I need? Currently, I have:

Cache-Control: max-age=86400 Content-Type: image/png 

It seems that the browser is not caching the image (it is about 20-30kb). What else would be necessary?

Edit: This is an example image, I already have an URL with .png extension: https://images.carspending.com/sigimg/5734/user/honda-accord-2-4i-executive-tourer_medium.png

like image 553
ddinchev Avatar asked Sep 06 '11 18:09

ddinchev


People also ask

How do I set up Cache-Control headers?

To use Cache-Control headers, choose Content Management | Cache Control Directives in the administration server. Then, using the Resource Picker, choose the directory where you want to set the headers. After setting the headers, click 'OK'.

Which headers can potentially allow a resource to be loaded from cache?

Cache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a resource is cached, where it's cached and its maximum age before expiring (i.e., time to live).

Are images automatically cached in browser?

does the browser cache images automatically? @Logan: Yes, the browser caches images automatically, provided your server sends the necessary headers to tell the browser it's safe to cache it. (These headers may also tell the browser the expiration time, which is part of your question.)

What should you add to a Cache-Control response header?

private. The private response directive indicates that the response can be stored only in a private cache (e.g. local caches in browsers). You should add the private directive for user-personalized content, especially for responses received after login and for sessions managed via cookies.


2 Answers

The final thing that worked was:

header('Pragma: public'); header('Cache-Control: max-age=86400'); header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400)); header('Content-Type: image/png'); 

Now the browser does not make requests for the image when loading a page with embeded one.

like image 54
ddinchev Avatar answered Oct 28 '22 22:10

ddinchev


Make sure you also add public as so:

header('Cache-Control: max-age=86400, public'); 

Read this also, is very helpful.

like image 45
Icarus Avatar answered Oct 28 '22 22:10

Icarus