2

I have a PHP file that randomly generates an image from a folder on every refresh. I downloaded it from here (which also has an explanation).

Instead of randomly choosing images, how can I have it change the image hourly? For example, I would like it have the same image for an hour, and then change when that hour is up. Basically, a new image based on some time interval.

Thanks for the help.

Sosa
  • 814
  • 1
  • 9
  • 20
  • Just to clarify: You want everyone in that hour to view the same image. You're not concerned with making the image change if the page has been left up over an hour. Also: do you want it to randomly choose the image every hour, or do you plan on having 24 images selected for every hour of the day? – Rhyono Sep 10 '12 at 02:37
  • If possible, I would like for everyone to view the same image. I am also not concerned about the image changing if the page is left the same. I would like for it to be random, but if I must choose 24 and duplicate a few images I will. I want it so that if I load the page an hour later there will be something else there. – Sosa Sep 10 '12 at 02:52
  • The reason random does not work when trying to display the same image to everyone is that you would have to have it randomly chosen, that choice saved, then that choice relayed every time someone views the page. Essentially, it gets much messier and requires additional pages. If you go with the method of choosing images, it can be any number up to 24. So if you only have 8 images, that is fine. – Rhyono Sep 10 '12 at 03:08
  • I see. In that case, what's the simplest way to have it choose an image instead from a folder. I'm working off the code from that link I posted. – Sosa Sep 10 '12 at 03:35
  • His code was very straightforward, so I ended up just switching out his time function, which he was using for the random number. It should work as you want now. – Rhyono Sep 10 '12 at 03:57

3 Answers3

1

Find line

$imageNumber = time() % count($fileList);

And replace it with

$imageNumber = (date(z) * 24 + date(G)) % count($fileList);

That should work for you.

Rhyono
  • 2,420
  • 1
  • 25
  • 41
0

I'd say you need a random oracle function. Basically, it's a random() function that takes an input and generates a random number, with the guarantee that all calls with the same input will give the same output.

To create the value you pass into the oracle, use something that'll change hourly. I'd use julian_day_number * 24 + hour_number or something of that variety (just hour_number isn't good enough, as it'll repeat itself every 24 hours).

Then, whenever your page loads, generate your hour number, pass it through your oracle, and use the result just like you use your random value now. It'll still appear random, and it'll change once an hour.

Hope that helps!

Edit: Random oracles don't need to be fancy - they can be as simple as (stolen blatantly from this answer to a different question):

int getRand(int val)
{
    //Not really random, but no one'll know the difference:
    return ((val * 1103515245) + 12345) & 0x7fffffff;
}
Community
  • 1
  • 1
Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
0

Keeping it simple, put 8 different pics in img/ named from 1.jpg to 8.jpg, then:

$imagePath = sprintf("img/%s.jpg", (date('G') %8) +1);

with G param being:

24-hour format of an hour without leading zeros.

Now you are sure that you have a different pic every hour, and everybody sees the same.

EDIT: narrow or widen the repetition period adjusting modulo, 24 has a few divisors [1, 2, 3, 4, 6, 8, 12].

moonwave99
  • 21,957
  • 3
  • 43
  • 64