如何强制浏览器刷新使用 imagecreatefromjpeg
创建的图像 ?
我知道这个SO问题(PHP强制刷新图像),如果我有一个真实的图像,但我需要刷新我刚刚创建的图像,因为关于用户操作(突出显示图像上的不同位置)图像可能不会刷新,我必须手动强制浏览器通过STRG + F5
重新加载它例如:- 前提条件:浏览器没有缓存
- 我登录到userA帐户,打开包含图像的页面并突出显示其中的一些点->图像创建正确
- 我退出并登录到userB并打开包含图像的页面并突出显示其中的一些点->我看到来自userA的旧图像
- 我必须按STRG + F5才能看到我的 的正确图像
- 结论:浏览器缓存图像而不是重新创建它
主页(由用户打开)包含以下部分:
<img src="http://<URL>/<dir>/draw.php">
draw.php
文件通过:
ob_start();
// get $results (= points to highlight) from database - works fine
$img = draw_stars_into_small_starmap($results);
ob_end_clean();
header("Content-type: image/jpeg");
imagejpeg($img, null, 85);
imagedestroy($img);
这个函数突出显示图像上的一些点
// $smallimg is the image from
// Results containing the points to highlight
function draw_stars_into_small_starmap($results) {
$smallimg = dirname(__FILE__) . "/nameofpicture.jpg";
// Which color for highlighting
$colormap = array(
'a' => array(240, 120, 120),
'o' => array(255, 128, 0),
'm' => array(0, 255, 0)
);
$width = 450;
$height = 100;
while ($row = od_mysql_fetch_array ($results)) {
$x = $row['xkoord'] / (610 / $width);
$z = $row['zkoord'] / (610 / $height);
$y = $row['ykoord'] / 5;
list($r, $g, $b) = $colormap[$row['type']];
$color = imagecolorallocate($smallimg, $r, $g, $b);
$x = floor($x);
$y = floor($y) + 50;
imagearc($smallimg, $x, $y, 9, 9, 0, 270, $color);
imagecolordeallocate($smallimg, $color);
}
return $smallimg;
}
有人知道吗?
注:我只有想总是强制浏览器刷新这个特定的图像(或绘图。php图像被放置),但不是网站/服务器上的所有图像!
您要做的是为图像添加一个随机数生成器,您想要刷新:
<?php
$rand = mt_rand(11111111,99999999);
?>
<img src="http://<URL>/<dir>/draw.php?<?php print $rand;?>">
这将强制HTML文档刷新浏览器地址,因为它调用的图像字符串由于随机数而发生了变化。
你可能还应该使用:
PHP
clearstatcache();
在您的<img>
包含页面在页面的顶部,以便每次页面加载它清除其文件缓存。这将在图像draw.php的实际内容没有正确刷新时使用。还有其他各种方法(
.htaccess
, header等)也可以设置您的服务器强制浏览器一般不缓存图像文件(尽管这可能会显着增加您的带宽)