Closed Thread Icon

Topic awaiting preservation: Timed randomness? (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=13002" title="Pages that link to Topic awaiting preservation: Timed randomness? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Timed randomness? <span class="small">(Page 1 of 1)</span>\

 
Darkwind
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2001

posted posted 11-25-2003 04:11

Hello.

I've looked though the FAQ and the other posts and I can't seem to find any info on this. If I missed it, My apologies.

What I am attempting to do is write a small script that will change an image randomly. (No big deal, I know).

However, the twist is I need the image to be random, but once selected, it remains locked to a specific amount of time.

Here is the break down of the problem.

1. To identify one group from another, they wish to use an image or avatar.
2. In order to keep this image from just being copied and uploaded they wish the images to be random but each member MUST have the same random image at the same time.
3. I will have a list of 24 images to pool to shuffle around.

My solution was to tie a randomly generated image to a specific 5 minute section of any given hour. 0-5 would have a random image from the 24, the next 5 minutes, 5-10 would have another randomly generated image and so forth.

The problem is, with each 'refresh' a random image is generated.

The other problem is the script will have to reside on a separate server from the forum server. So, each time a poster refreshes the forum, a new call to the script will be generated, hence a new random image.

Is it possible to cache an image to a specific section of an hour?

BTW. I've been doing this with some minor time calculations and a series of if statement.

The code is below:

code:
$minutes=((($mytime%604800)%86400)%3600)/60; 
$myminutes = round($minutes);

echo ("<br>" . $myminutes);

if($myminutes > 0){
if($myminutes < 5){
$imagevar=rand(1, 24);
echo "<br>0 to 5";
}
}
if($myminutes > 5){
if($myminutes < 10){
$imagevar=rand(1, 24);
echo "<br>5 to 10";
}
}
if($myminutes >= 10){
if($myminutes < 15){
$imagevar=rand(1, 24);
echo "<br>10 to 15";
}
}
if($myminutes >= 15){
if($myminutes < 20){
$imagevar=rand(1, 24);
echo "<br>15 to 20";
}
}
if($myminutes >= 20){
if($myminutes < 25){
$imagevar=rand(1, 24);
echo "<br>20 to 25";
}
}
if($myminutes >= 25){
if($myminutes < 30){
$imagevar=rand(1, 24);
echo "<br>25 to 30";
}
}
if($minutes >= 30){
if($myminutes < 35){
$imagevar=rand(1, 24);
echo "<br>30 to 35";
}
}
if($myminutes >= 35){
if($myminutes < 40){
$imagevar=rand(1, 24);
echo "<br>35 to 40<br>";
echo $imagevar;
}
}
if($myminutes >= 40){
if($myminutes < 45){
$imagevar=rand(1, 24);
echo "<br>40 to 45";
}
}
if($myminutes >= 45){
if($myminutes < 50){
$imagevar=rand(1, 24);
echo "<br>45 to 50";
}
}
if($myminutes >= 50){
if($myminutes < 55){
$imagevar=rand(1, 24);
echo "<br>50 to 55";
}
}
if($myminutes >= 55){
if($myminutes < 60){
$imagevar=rand(1, 24);
echo "<br>55 to 60";
}
}



Is this possible, or is there a flaw in My logic?

Darkwind
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2001

posted posted 11-25-2003 04:15

BTW, the echo statements are My debuging statements. I haven't wrote a call to an image yet.

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 11-25-2003 04:50

simply have one function ot randomly choose an image from an array/flat-file database/MySQL database/whatever. Then, after you've exited your script at the bottom, add the usual HTML tags (HTML, HEAD, TITLE, BODY, etc). Inside the head tag put a piece of javascript code (which someone else will have to supply) that refreshes the page every 5 seconds or whatever you want. Insie the body tag put a standard image tag that calls the image name out of your PHP script, like: <img src="http://www.mysite.com/images/<?PHP echo $image_name; ?>">. Then, when you're page refeshes, PHP randomly picks another image, and replaces the 'echo $image_name;' with a different name.

Hope this helps

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 11-25-2003 05:03

Hmm.....

Here's one way to do it.

Create a directory inside which is all 24 images named 0.gif 5.gif 10.gif .... etc.
Besides the images create an empty file named whatever you like.

Now what you do is have a php script that looks at the last modification time of the empty file (filemtime function). If the modtime was in the last hour (ie if it's now 10:15 and the last modtime was 9:15 just compare the 10 to the 9) than read all the image file names into an array, shuffle it and rename all the images. After you do this you can use touch to change the access time of the empty file.

If the file has been updated in this hour you just output the correct image using the header function.


Let us know if you need more specifics. but this method should do the trick.



.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 11-25-2003).]

[This message has been edited by bitdamaged (edited 11-25-2003).]

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 11-25-2003 05:45

Hey, good implementation, bitdamaged. I didn't know about the "touch" function -- that's useful stuff. I never really thought about it before, but empty files are a perfect way to set site-wide persistent "variables" in any number of circumstances.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

Darkwind
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2001

posted posted 11-25-2003 22:32

Okay, I think I was unsuccessful in getting My idea across. Maybe if I start out with the end product first.

What I want to do is call in image from a separate server using a simple image call.

&ltimg src=Path to image?username=user&password=pass&img=random image&gt

The directory were the images and scripts are going to be stored will be authenticated through the imgage call. This will keep those who do manage to find the files from accessing them directly.

I'm not sure I'm being clear here.

This image call is going to reside in a signature line in a forum program, specifically, phpBB.

What I want to do is place this image call in the signature line, and have this call produce a random image. The trick is, this random image MUST be the same image for everyone who is using the image call.

So, if at 12:05, image 12.gif is the random image, and every user must have image 12.gif showing.

At 12:10, the image must shift to a new image, say image 23.gif, but everyone must have image 23.gif for that five minute time period.

I guess what I'm saying is I want a group image that reshuffles every 5 minutes. I'd like this shuffle to be random.

Would it be best to do a random shuffle of the images every hour and then assign them to an array that consisted of the five minute intervals? I'd need 12 fields in the array that would be assigned something like;

time[0to5]
time[5to10]

etc...

Populate this array with a random shuffle that occurs every hour?

This is supposed to be a quick script just to help out some friends...

*LOL*


bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 11-25-2003 23:30

That's what this would do.

What you may be missing is that you can call php scripts in an image tag

code:
<img src="http://yoursite.com/sigthing.php">



And output an image with it like so

code:
$img = "path/to/img.gif";
$imgsize = getimagesize($img);
$fp = fopen ("path/to/img.gif", rb);
header("Content-type: {$imgsize['mime']}");
fpassthru($fp);
exit;




So if you write sigthing.php in 3 parts using the techniques above

1. Check if images have been shuffled in this hour. If not then shuffle them.
2. Figure out the image for this 5 minute set.
3. Output the correct image.

Then all is well.



.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 11-25-2003).]

Darkwind
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2001

posted posted 11-26-2003 01:11

Your correct. I did not know that you could call a script with an image tag.

I'll give that a shot!

Thanx!

Darkwind
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2001

posted posted 11-26-2003 05:52

Hey bitdamaged...

Just wanted to let you know that it worked!

Now, do you have any pearls of wisdom on how I can hid the image call so that people can't find the script by right clicking the picture and choosing properties?

Other than the DHTML "no right click" script? I don't think that will run in a forum signature line.



[This message has been edited by Darkwind (edited 11-26-2003).]

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 11-26-2003 05:57

since you call the image from the script, you get the script ot return the image. It should replace with it...or you can used .htaccess to mask it.

With .htaccess you can redirect all calls to http://www.yoursite.com/images/display_image.jpg with your script, while masking it with display_image.jpg

Darkwind
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2001

posted posted 11-26-2003 22:47

I'm sorry. You completely lost Me with that one.

I'm feeling really stupid here.

Have you got a breakdown on how I would do this?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 11-27-2003 03:16

I'm not great with mod_rewrite but I think what you can do is drop an .htaccess file into your signature directory. With the following.


code:
RewriteEngine  on
RewriteBase /~quux/
RewriteRule ^signature.gif$ sigthing.php



What this should do is internally change any requests for signature.gif to direct to sigthing.php






.:[ Never resist a perfect moment ]:.

« BackwardsOnwards »

Show Forum Drop Down Menu