0

私はしばらくコーディングしておらず、この問題の最も簡単な解決策を見つけようとしています。

サーバー上に20以上の画像(1.png、2.png、3.pngなどのラベルが付いています)があり、PHPを使用して一度に3つの画像を自分のWebサイトにレンダリングしています。

私が欲しいのは、12時間ごとに新しい画像が古い画像の1つを置き換えるようにすることです。たとえば、画像1、2、3をレンダリングすることから始まり、12時間後に2、3、4に切り替わります。

この効果を達成するための最良の方法は何ですか?

4

9 に答える 9

2

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/>";
}
于 2013-02-01T15:14:36.257 に答える
2

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);
于 2013-02-01T15:28:06.790 に答える
0

最良の方法は、置換を行い、12時間ごとにcronによって実行されるphpスクリプトを作成することです。

于 2013-02-01T15:06:47.837 に答える
0

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.

于 2013-02-01T15:08:13.180 に答える
0

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.

于 2013-02-01T15:10:28.353 に答える
0

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.

于 2013-02-01T15:11:06.657 に答える
0

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
于 2013-02-01T15:11:08.577 に答える
0

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 :-)

于 2013-02-01T15:11:20.933 に答える
0

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';
}
于 2013-02-01T15:14:59.310 に答える