php PNG图像输出的标题以确保它在浏览器中缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7324242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:29:29  来源:igfitidea点击:

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

phpcachinghttp-headersbrowserpng

提问by ddinchev

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.

我有动态生成的图像 (PNG),它们将嵌入网站和论坛中。当图像被张贴在一个非常繁忙的页面上时,有很多连接可以为不经常更改的内容提供服务。我想告诉浏览器缓存它多长时间。

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?

浏览器似乎没有缓存图像(大约 20-30kb)。还需要什么?

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

编辑:这是一个示例图像,我已经有一个带.png扩展名的 URL :https: //images.carspending.com/sigimg/5734/user/honda-accord-2-4i-executive-tourer_medium.png

回答by ddinchev

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.

现在,浏览器在加载嵌入的页面时不会请求图像。

回答by Icarus

Make sure you also add public as so:

确保您还添加了 public :

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

Read this also, is very helpful.

也读一下,很有帮助。

回答by ceejayoz

An Expiresheader should help.

一个Expires标题应该有所帮助。

header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));