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

后端 未结 3 861
独厮守ぢ
独厮守ぢ 2020-12-13 14:14

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 connectio

相关标签:
3条回答
  • 2020-12-13 14:57

    An Expires header should help.

    header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
    
    0 讨论(0)
  • 2020-12-13 15:01

    Make sure you also add public as so:

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

    Read this also, is very helpful.

    0 讨论(0)
  • 2020-12-13 15:09

    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.

    0 讨论(0)
提交回复
热议问题