Headers for PNG image output to make sure it gets

2020-02-08 02:47发布

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

3条回答
Evening l夕情丶
2楼-- · 2020-02-08 03:27

Make sure you also add public as so:

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

Read this also, is very helpful.

查看更多
何必那么认真
3楼-- · 2020-02-08 03:27

An Expires header should help.

header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
查看更多
\"骚年 ilove
4楼-- · 2020-02-08 03:39

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.

查看更多
登录 后发表回答