random image code
10 posts
• Page 1 of 1
random image code
so i have this code to look into a specified folder, generate a random number and grab the corresponding image, then output the resault as a 300x300px image. my only task now is to, instead of every refresh, have the image change every week. can anyone help with this?
here is the code for the random Image.
- Code: Select all
/*define the images directory*/
$dir = './artists/portfolios/dion/';
$dh = opendir($dir);
/* The following loop scans the directory specified ignoring folders and Thumbs.db */
while (false !== ($filename = readdir($dh))) {
if($filename == "Thumbs.db" || is_dir($filename)){
}else{
$files[] = $filename;
}
}
closedir($dh);
/* Generate a random number */
$nooffildi = count($files);
$nooffiles = ($nooffildi-1);
srand((double)microtime()*1000000);
$randnum = rand(0,$nooffiles);
/* print the result */
echo "<img src='$dir/$files[$randnum]' alt='$files[$randnum]' title='$files[$randnum]' width='300' height='300'>";
Last edited by c0ldfyr3 on Sat Sep 01, 2007 1:37 am, edited 1 time in total.
You can probably find a script on hotscripts that uses a date function to rotate the image on a weekly basis. Though I have to admit I've never seen anything like that. Anything that requires the server to keep track of that kind of time might be difficult to find, at least without a system that has a date/time tracking system.
"Greatness is not a function of circumstance. Greatness, it turns out, is largely a matter of conscious choice, and discipline." - Jim Collins
- kanaloa
- President
- Posts: 11795
- Joined: Sun Mar 10, 2002 1:18 am
- Location: HI & SC
- Real Name: John Derrick
what i found on hotscripts was a daily changer, the issue was that i needed to change the images to monday, tuesday, wednesday, etc and i am working with over 5000 images, so that wont do. if i need to add some sort of tracking system i can do that, they just want the front page "features of the week" to change every week. and as they dont know anything about coding ( html, php, cgi, perl etc ) i have to make a script do the job for them. all they know how to do is drop photos in their rightful folders.
Hmm, how much control do they have over things on the server end? They may have access to the functionality of a Cron daemon that could run a script to pick a random image and then write a file with the little bit of needed code for the image itself, and then just do an include for that file on your page.
Outside of that option, write a script to do the same as above, then where you include it on your page, have an "if" that compares the date on the file with the image reference, and given it's more than a week old, regenerate it before parsing the include
Outside of that option, write a script to do the same as above, then where you include it on your page, have an "if" that compares the date on the file with the image reference, and given it's more than a week old, regenerate it before parsing the include
- poisonbl
- Posts: 432
- Joined: Mon Nov 15, 2004 10:02 pm
- Location: WVU -- Morgantown, WV. -- USA (TZ: -5 hrs GMT, -4 DST)
okay i found a php command to get the date
however i am not sure if this is looking right... some help please..
here is the new script:
- Code: Select all
getdate(date("D"))
however i am not sure if this is looking right... some help please..
here is the new script:
- Code: Select all
/* Customise this line to change to your images folder */
$dir = './artists/portfolios/dion/';
$dh = opendir($dir);
$date=getdate(date("D"));
function date()
if($date == "tuesday");
printimage();
else
getfolderinfo();
function getfolderinfo()
/* The following loop scans the directory specified ignoring folders and Thumbs.db */
while (false !== ($filename = readdir($dh))) {
if($filename == "Thumbs.db" || is_dir($filename)){
}else{
$files[] = $filename;
}
}
closedir($dh);
function randomize()
/* Generate a random number */
$nooffildi = count($files);
$nooffiles = ($nooffildi-1);
srand((double)microtime()*1000000);
$randnum = rand(0,$nooffiles);
function printimage()
/* print the result */
echo "<img src='$dir/$files[$randnum]' alt='$files[$randnum]' title='$files[$randnum]' width='300' height='300'>";
Let me work something up quickly. What you need to do is store the information in a cookie on the users system or store the information in a file on your server. The file could be named the date the last image was rotated. Inside the file it could contain the image name/path.
Let me see what I can do.
Let me see what I can do.
- jbullard
- VP - Software
- Posts: 3653
- Joined: Sun Jun 06, 2004 10:17 pm
- Location: Utah
- Real Name: Jason Bullard
I worry a little when people define functions with names like date() just after calling a language builtin function named date() ... just what comes to mind for your needs, and based (loosely) on your code:
The reason for the separate php file to contain the image reference is that without that, there's no central place to hold when the last random image was picked, or what it was. Also, as a random note, you'll have to hand create the reference php file to start, as the script will fail when it finds that it doesn't exist. Also, I don't guarantee my code to work cleanly, as I don't have apache+php installed here at the moment and am just working from google, memory, and sleep deprived programming logic (which also means the code above probably needs a lot of error handling added)
** just one last catch, where it writes to the php file, you'll have to remove the spaces in the "<php>" sections, had to do that for it not to show as "<php>" on the forums :
((EDIT: I need to check to see if others post before I submit long writeups like this ))
- Code: Select all
<php>= $one_week) ||
(($date_now - $date_last_image) < 0)) {
set_new_image($dir,$image_reference);
}
/* Now, call the php file that we checked (and
possibly regenerated) above, which simply
echoes the html for the image to the client's
browser. */
include $image_reference;
?>
The reason for the separate php file to contain the image reference is that without that, there's no central place to hold when the last random image was picked, or what it was. Also, as a random note, you'll have to hand create the reference php file to start, as the script will fail when it finds that it doesn't exist. Also, I don't guarantee my code to work cleanly, as I don't have apache+php installed here at the moment and am just working from google, memory, and sleep deprived programming logic (which also means the code above probably needs a lot of error handling added)
** just one last catch, where it writes to the php file, you'll have to remove the spaces in the "<php>" sections, had to do that for it not to show as "<php>" on the forums :
((EDIT: I need to check to see if others post before I submit long writeups like this ))
- poisonbl
- Posts: 432
- Joined: Mon Nov 15, 2004 10:02 pm
- Location: WVU -- Morgantown, WV. -- USA (TZ: -5 hrs GMT, -4 DST)
I worry a little when people define functions with names like "date" just after calling a function named the same ... just what comes to mind for your needs, and based (loosely) on your code:
The reason for the separate php file to contain the image reference is that without that, there's no central place to hold when the last random image was picked, or what it was. Also, as a random note, you'll have to hand create the reference php file to start, as the script will fail when it finds that it doesn't exist. Also, I don't guarantee my code to work cleanly, as I don't have apache+php installed here at the moment and am just working from google, memory, and sleep deprived programming logic
((EDIT: I need to check to see if others post before I submit long writeups like this lol))
((EDIT2: php based forums and long segments of php don't play nicely, should be fixed now ... maybe, and sorry for the double post))
- Code: Select all
/* This function takes in the directory of the images and the
name of the php file to write, picks a random image, and
dumps the needed info into the php file, overwriting it. */
function set_new_image($img_dir,$out_file) {
$dh = opendir($img_dir);
$fh = fopen($out_file, 'w') or die("can't open file");
/* The following loop scans the directory specified ignoring folders and Thumbs.db */
while (false !== ($filename = readdir($dh))) {
if($filename == "Thumbs.db" || is_dir($filename)){
}else{
$files[] = $filename;
}
}
/* Generate a random number */
$nooffildi = count($files);
$nooffiles = ($nooffildi-1);
srand((double)microtime()*1000000);
$randnum = rand(0,$nooffiles);
/* Write the new php segment to the file */
fwrite($fh,
"echo \"<img src='$img_dir/$files[$randnum]' alt='$files[$randnum]' title='$files[$randnum]' width='300' height='300'>\"\n");
/* Close all file handles */
closedir($dh);
fclose($fh);
}
/* This is where actual code starts executing,
at least until the above is called. */
$dir = './artists/portfolios/dion/';
$image_reference = './weekly_image.php';
/* get the current time and the time the image was last set,
just keep both as Unix timestamps so we can do a little
math with 'em. */
$date_now = time();
$date_last_image = filemtime($image_reference);
# figure out what 7 days is in seconds
$one_week = 7 * 24 * 60 * 60;
/* If it's been more than a week, or times are messed up
(negative difference between now and when it was last set)
set a new image for display */
if( (($date_now - $date_last_image) >= $one_week) ||
(($date_now - $date_last_image) < 0)) {
set_new_image($dir,$image_reference);
}
/* Now, call the php file that we checked (and
possibly regenerated) above, which simply
echoes the html for the image to the client's
browser. */
include $image_reference;
The reason for the separate php file to contain the image reference is that without that, there's no central place to hold when the last random image was picked, or what it was. Also, as a random note, you'll have to hand create the reference php file to start, as the script will fail when it finds that it doesn't exist. Also, I don't guarantee my code to work cleanly, as I don't have apache+php installed here at the moment and am just working from google, memory, and sleep deprived programming logic
((EDIT: I need to check to see if others post before I submit long writeups like this lol))
((EDIT2: php based forums and long segments of php don't play nicely, should be fixed now ... maybe, and sorry for the double post))
- poisonbl
- Posts: 432
- Joined: Mon Nov 15, 2004 10:02 pm
- Location: WVU -- Morgantown, WV. -- USA (TZ: -5 hrs GMT, -4 DST)
10 posts
• Page 1 of 1
Return to HTML, CSS, and Scripts
Who is online
Users browsing this forum: No registered users and 5 guests