Reply LinkBack Thread Tools Search this Thread
Old 08-04-2007, 12:00   #1 (permalink)
helpmeplz
Needs help
 
helpmeplz's Avatar
 
Join Date: Sep 2004
Posts: 26
Is it possible to generate one random image every 24 hours?

I want to create a website that is just a picture and maybe a line of text underneath, explaining what the pictures are. Is there a script or something that I can use so that every 24 hours, one random image is shown from a directory including ~300 pictures?

The text does not have to change, just the image. The image must remain the same for all browsers for a set amount of time (probably 24 hours) and then when the time is up, another random image is shown. The image can be a random one plucked from the 300, or they can go in order. Random is preferred so that repeats can happen before every image has been displayed.

(Obviously I'll change the CHMOD settings so that browsers can't view the images in advance. Naughty naughty people.)

I've just drawn this in paintshop to show you what I sort of mean:

If I've forgotten to hide something from my browser that you shouldn't be seeing, ignore it.

The image in the middle is obviously the one that is randomly generated.

If someone can provide me with a way of doing this effectively, I would be most thankful.

NB. If what I desire is not possible, can someone please point me towards a nice and easy script that would change the image everytime the page is reloaded. I really don't want to go for this option but if it seems like the best alternative - let me know if there's something else I could do.
  Reply With Quote
Old 08-04-2007, 12:37   #2 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,340
I'd take a look at cron jobs, which are a feature of Unix (and Linux) servers. They allow you to take time-sensitive server actions (such as, backing up your database every 48 hours or something).

You might consider setting one up that could take an image from a folder and place it in another folder. That way you always link to the same image, but the image that's in the folder changes every 24 hours.

Not sure how you'd do the description. Maybe the same process, but with an include file...
  Reply With Quote
Old 08-04-2007, 13:08   #3 (permalink)
steveb
389 ppm and rising
 
steveb's Avatar
 
Join Date: Aug 2005
Location: Järvenpää, Finland
Posts: 5,484
I know next to nothing about PostSCript, so forgive me if this sounds ridiculous to those in the know.
If you name all your images from "001.jpg" to "300.jpg", would it not be possible to generate a random number between 1 and 300 and feed that result into the image name selector?
  Reply With Quote
Old 08-04-2007, 16:18   #4 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,340
Certainly. However, he wants it to change every 24 hours. Not every time a page loads. Showing a random image is child's play. Showing one at specific intervals in time is teenagers' play. What a lousy metaphor.

Not sure what PostScript has to do with it...
  Reply With Quote
Old 08-04-2007, 16:27   #5 (permalink)
steveb
389 ppm and rising
 
steveb's Avatar
 
Join Date: Aug 2005
Location: Järvenpää, Finland
Posts: 5,484
"Not sure what PostScript has to do with it..."

I probably mean Javascript or summat, sorry!
  Reply With Quote
Old 08-04-2007, 19:12   #6 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,340
If you were to do what you posted, a server-side language like PHP or ASP.NET would be the best bet.
  Reply With Quote
Old 09-04-2007, 15:51   #7 (permalink)
WeeJames
Dog Fighter
 
WeeJames's Avatar
 
Join Date: Oct 2006
Location: Glasgow.
Posts: 50
Should be fairly straightforward. Take the number of days passed since Jan 1st 2007 (or any other arbitrary date) then mod it by the number of images you have. You could always throw in some other crazy calculations before the mod to totally randomize it.

Then name your images 0001.jpg, 0002.jpg etc.
  Reply With Quote
Old 09-04-2007, 16:28   #8 (permalink)
BenSky
Registered User
 
BenSky's Avatar
 
Join Date: Sep 2006
Location: Leeds
Posts: 97
Quote:
Originally Posted by WeeJames
Should be fairly straightforward. Take the number of days passed since Jan 1st 2007 (or any other arbitrary date) then mod it by the number of images you have. You could always throw in some other crazy calculations before the mod to totally randomize it.

Then name your images 0001.jpg, 0002.jpg etc.

Yeah, just do it in php, assign an image to a day of the month.. bobs your uncle.
  Reply With Quote
Old 09-04-2007, 17:03   #9 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,340
Now that I think of it, you could name the images something like: YYYY-MM-DD.jpg

Then something akin to:

HTML Code:
<img src="<?php echo date('Y-m-d'); ?>.jpg" alt="AltText" />
Not 100% sure on the date format, but it's much simpler than worrying about cron jobs.

Or even just "m-d". The year could be included to make it work across different years.
  Reply With Quote
Old 09-04-2007, 23:25   #10 (permalink)
gk
geek
 
gk's Avatar
 
Join Date: Oct 2006
Location: *.everywhere
Posts: 204
Send a message via ICQ to gk Send a message via AIM to gk Send a message via MSN to gk Send a message via Yahoo to gk
If you're files are sequentially named (0001.jpg ,0002.jpg...) and you know what the number of the last image is.

you can do something like this:

PHP Code:
<?
$last_image
=2000;
srand(date('mdy'));
$image_number=rand(1$last);
?>

<img src="image_dir/<?=$image_number?>.jpg" alt="" />

Seeding with date('mdy') will cause rand to spit out the same number until the date changes. this way you don't need to have an image save specifically for that day.

or if it's just directory of images you can create and array of all the files in that dir and and use rand to select a key form the array. the problem there is every time the page load the directory will be read so you may want to put the list of file in to a text file or some intermediate step to prevent repeatedly reading the directory
  Reply With Quote
Old 10-04-2007, 17:34   #11 (permalink)
helpmeplz
Needs help
 
helpmeplz's Avatar
 
Join Date: Sep 2004
Posts: 26
I'll try that, thank you.
  Reply With Quote
Old 10-04-2007, 18:00   #12 (permalink)
helpmeplz
Needs help
 
helpmeplz's Avatar
 
Join Date: Sep 2004
Posts: 26
I don't know php at all. Can you please explain what I need to do? :/




edit: http://pr0xay.freehostia.com/image.html
ehm..

Last edited by helpmeplz : 10-04-2007 at 18:14.
  Reply With Quote
Old 10-04-2007, 20:10   #13 (permalink)
inpixel
www.inpixel.co.uk
 
Join Date: Apr 2007
Posts: 10
you need to save ur page with the extension .php (go file>Save as and type index.php)

copy that code from above into your documents, and use the img code where u want the image.

Ash
  Reply With Quote
Old 11-04-2007, 05:21   #14 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,127
fwiw you're probably better of using date('z') which is the day of the year (starting from 0)
__________________
George Smith - a vain attempt to get higher on google for my name

  Reply With Quote
Old 11-04-2007, 09:12   #15 (permalink)
helpmeplz
Needs help
 
helpmeplz's Avatar
 
Join Date: Sep 2004
Posts: 26
Thank you inpixel.

One problem I seem to have is the image name that it generates.

PHP Code:
<?
$last_image
=2000;
srand(date('mdy'));
$image_number=rand(1$last);
?>

<img src="image_dir/<?=$image_number?>.jpg" alt="" />
was spitting out "1.jpg"

The problem there is the images are named 001.jpg upto 054.jpg. The only fix I know was to change the last line to
PHP Code:
<img src="image_dir/00<?=$image_number?>.jpg" alt="" />
so it changed to 001.jpg. However, when it gets past number nine it'll give me 0010.jpg and not 010.jpg. How do I resolve this?

Like I've said the last image at the moment is 054 as I've not done enough pictures to last a whole year. With gk's code, what'll happen in 55 days time? I really don't understand how it works, or why it's given me number one first, so hopefully you can shed some light on this.

Thanks a lot for the help so far.
  Reply With Quote
Old 11-04-2007, 09:29   #16 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,127
printf is your friend

Code:
<img src="image_dir/<? printf("[%03s]\n", $image_number); ?>.jpg" alt="" />

PHP: sprintf - Manual
__________________
George Smith - a vain attempt to get higher on google for my name

  Reply With Quote
Old 11-04-2007, 09:35   #17 (permalink)
helpmeplz
Needs help
 
helpmeplz's Avatar
 
Join Date: Sep 2004
Posts: 26
I feel so thick!

I changed it to:
PHP Code:
<img src="images/<? printf("[%03s]\n",   $image_number); ?>.JPG" alt="pic of the day" />

But on the page source it comes out as
PHP Code:
<img src="images/[001]
.JPG" 
alt="pic of the day" /> 

And the pic doesn't display.
  Reply With Quote
Old 11-04-2007, 10:02   #18 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,127
ah, sorry, copy pasted - you don't need the [ or the ] or the \n (newline) in the printf statement

Code:
printf("%03s", $image_number);
__________________
George Smith - a vain attempt to get higher on google for my name

  Reply With Quote
Old 11-04-2007, 22:28   #19 (permalink)
gk
geek
 
gk's Avatar
 
Join Date: Oct 2006
Location: *.everywhere
Posts: 204
Send a message via ICQ to gk Send a message via AIM to gk Send a message via MSN to gk Send a message via Yahoo to gk
Quote:
Originally Posted by stickmus
fwiw you're probably better of using date('z') which is the day of the year (starting from 0)

I think you may be right. I think "date('z')", may, provide better randomization.
  Reply With Quote
Old 12-04-2007, 16:28   #20 (permalink)
helpmeplz
Needs help
 
helpmeplz's Avatar
 
Join Date: Sep 2004
Posts: 26
Hmm, I think it has worked.

This is the code I used:
PHP Code:
<?
$last_image
=054;
srand(date('z'));
$image_number=rand(1$last);
?>

<img src="http://www.designerstalk.com/forums/images/<? printf("%03s"$image_number); ?>.JPG" border="1" alt="pic of the day" />

And this is the page: website

For me, it shows the picture 001.JPG. Is that supposed to be shown? Will it be different tomorrow?

If so, I better get more than fifty four images done A-Sap!

Thanks for the help (so far) everyone. Much appreciated.

Last edited by helpmeplz : 05-05-2007 at 14:09.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8