缓存动态生成的图像

发布于 2024-12-02 17:26:07 字数 785 浏览 2 评论 0原文

我正在使用 PHP 脚本生成一些图像。
我想缓存图像,这样浏览器就不必每次都加载它们。 我添加了这些标头:

'max-age' => 3600
'Etag' => md5($image->getSlug())
'last-modified' => $image->getUpdatedAt()
'Vary' => 'Accept-Encoding'
'content-length' => (size of the image)
'Content-type' => 'image/jpeg'

但是图像没有缓存,并且每次都会由浏览器加载。

响应标头如下所示(使用 Firebug):

Date             Sun, 04 Sep 2011 00:25:45 GMT
Server           Apache/2.2.16 (Debian)
X-Powered-By     PHP/5.3.3-7+squeeze1
Cache-Control    max-age=3600, private
Etag            "9280c6c672c6535c13b7481972f9ac39"
Last-Modified    Sat, 27 Aug 2011 01:36:24 GMT
Vary             Accept-Encoding
Content-Length   26231
Connection       close
Content-Type     image/jpeg

有人知道这里出了什么问题吗?

I am generating some images using a PHP script.
I want to cache the images, so the browser doesn't have to load them each time.
I have added these headers:

'max-age' => 3600
'Etag' => md5($image->getSlug())
'last-modified' => $image->getUpdatedAt()
'Vary' => 'Accept-Encoding'
'content-length' => (size of the image)
'Content-type' => 'image/jpeg'

but the image is not cached, and it is loaded every time by the browser.

The response headers look like these (using Firebug):

Date             Sun, 04 Sep 2011 00:25:45 GMT
Server           Apache/2.2.16 (Debian)
X-Powered-By     PHP/5.3.3-7+squeeze1
Cache-Control    max-age=3600, private
Etag            "9280c6c672c6535c13b7481972f9ac39"
Last-Modified    Sat, 27 Aug 2011 01:36:24 GMT
Vary             Accept-Encoding
Content-Length   26231
Connection       close
Content-Type     image/jpeg

Does anyone have any idea what is wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

≈。彩虹 2024-12-09 17:26:07

强制缓存图像的一种方法是将 Last-Modified 标记设置为您要使用的图像的创建日期和时间。例如:

<?php
error_reporting(0); //disable error reporting as it will corrupt the image
session_start(); //start a php session
$date = date(DATE_RFC822);
//some code to connect to a mysql database
$query = mysqli_query($link, "SELECT * FROM images WHERE sessionid='$sessionid'");
if ($query and mysqli_num_rows($query) == 1) { //if mysql returned one row
    $row = mysqli_fetch_array($query); //get the row as an array
    $date = $row["date"]; //set the date to the one in the database
} else { //if the user has never seen the image before
    mysqli_query($link, "INSERT INTO images SET sessionid='$sessionid', date='$date'"); //put the session id and date into the database for later use
}
header("Last-Modified: $date");
//some code that would generate and send the image

不要忘记创建一个数据库(在任何数据库程序中),其中包含一个名为 images 的表,其中 sessionid 应为主键,并且 日期。两者都应该是字符串。

One way you can force an image to be cached is to set the Last-Modified tag to the date and time that the image you want to use was created. For example:

<?php
error_reporting(0); //disable error reporting as it will corrupt the image
session_start(); //start a php session
$date = date(DATE_RFC822);
//some code to connect to a mysql database
$query = mysqli_query($link, "SELECT * FROM images WHERE sessionid='$sessionid'");
if ($query and mysqli_num_rows($query) == 1) { //if mysql returned one row
    $row = mysqli_fetch_array($query); //get the row as an array
    $date = $row["date"]; //set the date to the one in the database
} else { //if the user has never seen the image before
    mysqli_query($link, "INSERT INTO images SET sessionid='$sessionid', date='$date'"); //put the session id and date into the database for later use
}
header("Last-Modified: $date");
//some code that would generate and send the image

Don't forget to create a database (in any database program) that contains a table called images with sessionid, which should be the primary key, and date. Both should be strings.

江城子 2024-12-09 17:26:07

您需要计算并添加 Expires: 标头。例如:

Expires: Fri, 30 Oct 2011 14:19:41 GMT

You need to calculate and add an Expires: header. For example:

Expires: Fri, 30 Oct 2011 14:19:41 GMT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文