Forum Moderators: coopster

Message Too Old, No Replies

Random "pic o'the day"

Any simple way to display a random image each day?

         

Alligator Dream

11:56 am on Mar 18, 2005 (gmt 0)

10+ Year Member



I have searched Google and here using such terms as "php random day," but for the most part the only solutions I'm finding are either reeeeeally long and complex, or something similar to what I'm already doing.

Right now I've got this simple little gizmo on my page:

<?php
$img = date("z");
echo "<IMG SRC=\"images/".$img.".jpg\">";
?>

I've got a bunch of images in the "images" directory that are just numbered, 85.jpg, 86.jpg, and so on, so the above script displays a specific image every day. I've been trying to find a simple script that will let me randomize the displayed image, but only every day, and not every time the page is viewed. Is there any relatively uncomplicated way to do this? I'm still a newbie and very indimidated by long, involved scripts. So far all the fiddling around I've done with rand() results in stuff that would load a different image for every pageview, which I definitely don't want. My existing little script works for all intents and purposes, I'd just like to randomize it if possible.

saoi_jp

1:48 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



How many pictures do you have? If you have enough for a year, then your current method seems fine.

Or, if you've only got 31 images, then you could go by the number of the day of the month.

The problem is this: If you randomize for one pic a day, then basically you're just taking a picture and assigning it to a particular day. It's not really all that random -- it just changes every day.

When you say "long, involved scripts", how many lines are you talking about? Here are the steps I'd take if doing the script:

0. See if today has an image.
- 0.A) If so, show it.
- 0.B) If not, do 1-4:
1. open a connection with the image bin
2. read all the titles of the images.
3. randomly choose one.
4. assign that to today (by putting it into a MySQL database, or writing to a writable file)

There'll be a chance that some images get shown multiple times, while others remain obscure.

Alligator Dream

2:03 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



Well yeah, the current script is fine, but it isn't what I want. I'd like it to be random rather than "75.jpg will always be shown on Day 75, 87.jpg will always be shown on Day 87."

Your option B sounds like what I'm looking for. How would I go about implementing that, so that a randomly-selected image is shown every day? (I'm a little confused by the "problem" you mention; maybe I don't understand why it's a problem?)

saoi_jp

3:38 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



I don't understand why it's a problem

It's not a problem, really. I'm just saying that the end result is no different than your current solution. If today's picture is 76.jpg because today is the 76th day of the year, who's going to remember that next year on March 19, 2006? It's not really any different from choosing one picture at random for one day only (so that today's picture is 12.jpg, selected at random), except that your current solution saves on processor load.

Alligator Dream

4:06 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



I see what you're saying. Still, "because it's what I want to do" seems like a good enough reason to me. :)

Sooooo...Suggestions toward that end? I'd be grateful as always to any WW gurus who could point me in the right direction.

jatar_k

10:05 pm on Mar 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well the thing is, as was mentioned, random could show the same image 2 days in a row

here is what you could do

say we have 100 images each named, as you already have it, just with a number

we have a static link to an image called today.jpg in the root of the site

we select the file by cron nightly, the cron could be something like

we would need 2 dirs for this
one called active for images to be used
one called used for images that have been used already

1. see if there are any images in the active directory
2. if there aren't any move all of the images from used to active
3. load the names (numbers) of the images into an array
4. randomly select one number from the array
5. copy that image to the site root and name it today.jpg, overwriting the previous day's image
6. move the image to the used directory

that would keep you from using an image twice until every image is used. That would also allow you to add images anytime with out having to change the script.

Alligator Dream

11:03 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



I really don't mind if randomization occasionally resulted in the same image two days in a row, it's the randomization itself that I'm gunning for.

You had me until you got to "cron," unfortunately. I know absolutely nothing about cron jobs (I see where it is in my cPanel, but have no idea what to do with it). Can you elaborate, or would it be simpler to use a MySQL database in some way, as saoi_jp hinted at?

jatar_k

12:13 am on Mar 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



a cron is a script that you schedule to run

so, in this case, the script that does the image change could run every night at midnight and change you image. It's just a script, nothing special, it just happens to be run from your crontab on a schedule.

Alligator Dream

12:18 am on Mar 19, 2005 (gmt 0)

10+ Year Member



So I would...type the script in the little textarea that says "Command to run:"?

jatar_k

1:09 am on Mar 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I believe so, never set one up using cPanel

you may have to type in something like

/path/to/php /path/to/script.php

not sure but I am sure your host has docs on it

Alligator Dream

1:20 am on Mar 19, 2005 (gmt 0)

10+ Year Member



Okay, I will investigate in that direction. Thank you for the advice! :)

PogoDo

3:35 am on Mar 20, 2005 (gmt 0)

10+ Year Member



Here is some piece of code. It will show random image from a folder, regardless the name of the files:

[fixed]
[2]

<?php
function rndImage(){
$folder = "images"; /// Change this to your img folder
$imgFiles = Array();
$dirHandler = opendir($folder) or die("cannot opendir");
while($myImages = readdir($dirHandler)){
if($myImages == "." ¦¦ $myImages == ".."){continue;}
array_push($imgFiles,$myImages);
}
$imgNo = rand(0, count($imgFiles));
$imgName = $folder."/".$imgFiles[$imgNo];
echo $imgName;
}
?>

[/2]
[/fixed]


when you wan to put random img:


[fixed][2]<IMG SRC="<?php rndImage()?>">[/2][/fixed]

jatar_k

4:34 pm on Mar 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld PogoDo