在一定数量的页面加载后替换图像?

时间:2014-12-05 09:48:26

标签: php image

在一定数量的页面加载后如何替换图像?

我有一个图像文件夹(可以连续编号或在上传时添加一些内容),在任何用户加载X页面后,图像会发生变化,这只会继续浏览文件夹中的图像随着时间的推移得到补充。

提前致谢。

3 个答案:

答案 0 :(得分:0)

每次加载页面时,在数据库中增加一个计数器。 然后根据该计数器显示图像,例如:

imageIndex = (numberOfPageVisits / changeImageAfterSoManyVisits) % numberOfImages

答案 1 :(得分:0)

一种方法可能是设置一个cookie来计算用户访问网站的次数,一旦达到一定数量,就会显示新图像并且计数器会重新设置为0.

这些方面的东西:

if(!isset($_COOKIE['load_counter'])) {
    // Create cookie for counter
    $cookie_value = "0";
    setcookie("load_counter", $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day * 30 = 30 days
} else if($_COOKIE['load_counter'] < 5) { 
    // update cookie if its less than the 5th hit
    $cookie_val = $_COOKIE['load_counter'] + 1;
    setcookie("load_counter", $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day * 30 = 30 days
} else {
    // RUN NEW IMAGE HERE

    // Reset counter
    $cookie_value = "0";
    setcookie("load_counter", $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day * 30 = 30 days
}

不需要任何数据库连接,服务器的开销很小,而且前端速度非常快。

答案 2 :(得分:0)

这就是我最终做的事情:

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
}
return $theValue;
}
}

// get the images from the db
mysql_select_db($database_Database, $Database);
$query_rs_images = "SELECT * FROM image_degrade WHERE counter > 1 ORDER BY id ASC";
$rs_images = mysql_query($query_rs_images, $Database) or die(mysql_error());
$row_rs_images = mysql_fetch_assoc($rs_images);
$totalRows_rs_images = mysql_num_rows($rs_images);

$file = "images/gallery/" . $row_rs_images['filename'];

// resave the image at reduced quality
imagejpeg(imagecreatefromjpeg($file), $file, $row_rs_images['counter']);

// reduce the quality var by 1
$row_rs_images['counter'] = $row_rs_images['counter'] - 1;

// update the db
$updateSQL = sprintf("UPDATE image_degrade SET `counter`=%s WHERE id=%s",
                   GetSQLValueString($row_rs_images['counter'], "int"),
                   GetSQLValueString($row_rs_images['id'], "int"));

mysql_select_db($database_Database, $Database);
$Result1 = mysql_query($updateSQL, $Database) or die(mysql_error());

// output the image
header('Content-type: image/jpeg');
echo file_get_contents($file);

mysql_free_result($rs_images);
?>