缓存php生成的图像,直到更改为止 [英] Cache php-generated image until it changes

查看:58
本文介绍了缓存php生成的图像,直到更改为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我对这个主题的知识不足,我向您道歉;我看过很多其他的文章,但是找不到任何适合我的解决方案。

I apologize in advance for my lack of knowledge on this subject; I've looked at lots of other posts but can't get any of their solutions to work for me.

无论如何,我正在使用动态的 displayimage .php 文件,以在我正在处理的网站上显示用户的个人资料图片。该页面带有一个id参数,并从mysql数据库中提取图像文件名。下面是(缩写)代码:

Anyway, I'm using a dynamic displayimage.php file to show users' profile pictures on a site I'm working on. The page takes an id parameter, and pulls the image filename from a mysql database. Here's the (abbreviated) code:

$id = mysql_real_escape_string($_GET[id]);

$table = "images_user";
$idname = "userid";
$uploaddir = "/home/username/uploads/images/user/"; //outside web root

$select = mysql_query("SELECT * FROM $table WHERE $idname = '$id' LIMIT 1");

if(mysql_num_rows($select) > 0) {
  $file = mysql_fetch_assoc($select);

  header("Content-Type: $file[mimetype]");
  readfile($uploaddir.$file[name]);
}

当前,由于它是从php文件生成的,因此未缓存图像,这确实会使网站速度变慢。因此,我添加了以下内容:

Currently, because it's generated from a php file, the image is not being cached, which really slows the site down. So I add this:

header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));

哪个可以解决问题,太好了!现在,图像已被缓存,并且加载速度超级快。

Which solves the problem, great! Now the images are cached, and load super quickly.

但是,用户可以更改其个人资料图片。发生这种情况时,我将保存他们上传的图片,删除旧的图片,然后更新数据库条目以指向新的图片。但是现在,由于旧图片仍在缓存中,除非他们手动F5页面,否则他们看不到更新。

However, users have the ability to change their profile pictures. When this happens, I save their uploaded picture, delete the old one, and update the database entry to point to the new picture. But now, because the old picture is still cached, they don't see the update unless they manually F5 the page.

我如何获取它来缓存图片,

How can I get it to cache the picture, but force a 're-cache' when the picture has been changed?

推荐答案

更新图片后,添加一个额外的参数,但是在图片更改后强制执行重新缓存吗?到显示图像的URL,这里是 displayimage.php。
最常见的是时间戳记。

after they update the picture, add an extra parameter to the url you access to show the image, which here is `displayimage.php'. Most commonly this would be a timestamp.

通常情况下,您可以访问 displayimage.php?id = 123123

Like in normal case, you would access displayimage.php?id=123123

更新个人资料图片后,要刷新图像,您将需要访问
displayimage.php ?id = 123123& time = 1000121110
更改时间参数将在用户每次更新图像时强制从页面重新加载图像。

and after updating the profile pic, to refresh the image, you will need to access displayimage.php?id=123123&time=1000121110. The changing time parameter would force an image reload from the page everytime the user updates the image.

这篇关于缓存php生成的图像,直到更改为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆