I haven't coded in a while and I'm trying to find the easiest solution to this problem.
I have 20+ images on the server (labelled 1.png, 2.png, 3.png, etc.) and I'm using PHP to render three of them to my website at a time.
All I want is to make it so every twelve hours a new image replaces one of the old images. So for example it starts by rendering images 1, 2 and 3 and after twelve hours it switches to 2, 3 and 4.
What's the best way to achieve this effect?
This can be done in simple PHP (since you mentioned PHP).
$numOfImages = 20;
$unit = date('j') * 2; // Day of the month, doubled to simulate chunks of 12 hours.
if (date('G') < 12) { // Hours 0 - 23
$unit--;
}
$image1 = $unit % $numOfImages;
$image2 = ($unit + 1) % $numOfImages;
$image3 = ($unit + 2) % $numOfImages;
var_dump($image1);
var_dump($image2);
var_dump($image3);
The best way is to write php script which will do replacing and which will be executed by cron every 12 hours.
You can use a cron job to run a script that would increment the index on the image URL.
php * */12 * * * root/to/updateImage.php
The in the update image script just change a value in the database that refers to which image you are currently using.
If you can use cron jobs then I would create a symlink "current.jpg" and every 12 hours fire a script which executes a command to change the symlink to another one. Another option is to have a file "img_config.php" with a value:
$current_image = 3;
and every time cron hits do:
include "img_config.php"
$current_nr = (++$current_image)%20+1;
file_put_contents("img_config.php",'$current_image = '.$current_nr.';');
Of course you have got many other options like store that in a value in a database.
In PHP, select which images to show from a config file
, which may look like:
1.png
2.png
3.png
Then write a cron job with a shell script, which will modify the config file
. You can use sed
or awk
for it.
Cron, or a function with this logic on top of your app, if "exact hours" is not a concern:
Read content of last_change.txt
If the content *plus* 12 hours *is less than* current time
Change images
Put current time to last_change.txt
Done
I had the same problem with one image which should be chosen from a set of images and I didn't have cron access. So I came up with:
function changeStartpageImage() {
// change the image every x hours
$hours = 170;
$fileinfo = stat("images/startseite/startseitenbild.jpg");
if (time() - $fileinfo['mtime'] < ($hours * 60* 60)) {
return;
}
$handler = opendir("images/startseite/");
$results = array();
while ($file = readdir($handler)) {
if (substr($file, 0, 1) != "." && $file != "startseitenbild.jpg" ) {
$results[] = $file;
}
}
$newFile = $results[array_rand($results)];
copy("images/startseite/".$newFile, "images/startseite/startseitenbild.jpg");
closedir($handler);
}
I think you can elaborate on this example and make it even nicer, but it is a possibility to deal with the problem without having to deal with Cronjobs :-)
You don't need a cronjob, you can use the time()
function and use the mod
power for shift the images seen every 12 hours.
Try this script
$now = time(); // you can simulate adding 12 hours +3600*12
$base = round( $now/(3600*12) ); // this number change every 12 hours
$n_images = 20; // number of your images
$start_from = $base % $n_images; // start from $start_from image
// loop for get your 4 images (3,4,5,6 or 4,5,6,7 or ,18,19,20,1,2 etc.)
for($i = 0; $i < 4; $i++) {
$image = ($start_from+$i) % $n_images + 1;
echo "get image " . $image.".jpg<br/>";
}
I wouldn't run a cron. Here is how I would do it:
$num_images = 20;
$images_display = 3;
$hours = 12;
$time_block = 60 * 60 * $hours;
$time_now = strtotime('now');
$block_num = floor($time_now / $time_block) % $num_images;
$images = array();
for($x=1;$x<=$images_display;$x++) {
$image_num = $block_num + $x;
if($image_num > $num_images) $image_num = $image_num % $num_images;
$images[$x] = $image_num . '.png';
}