How to Create Random Header Images in PHP
OK, so like, I was stuck there wanting to make my new website look even cooler, so I thought, …wouldn’t it be neat if I could have random background images in my header. That way, whenever someone revisits or refreshes or visits and new page - VIOLA! - a new header image. Cool, eh? But then I thought - “I’ll have to use PHP to generate a random number or numbers, and thus generate random images…
Here’s how I did it:
- Create a series of images that are all the same size that you want to be your random images. Name them all the same except for an incrementing number just before the filename suffix (.gif, .jpg) - eg: imagename1.jpg, imagename2.jpg, imagename3.jpg, etc…
- Upload them to your chosen image folder
- Wherever you want you random images to appear (I used a div tag to create random background images for my header area), place your variation of the code below:
<div id='header' style=' <?php srand ((double) microtime( )*1000000); $random_number = rand(1,3); echo ('background: url(yourimage'. $random_number.'.jpg) top no-repeat;'); ?> '> <div id='content'> Content here. </div> </div> - The bit of code that says “rand(1,3);” denotes the choice of random numbers between 1 and 3. Change this to your requirements - eg: If you need numbers between 5 and 15 - change it to: “rand(5,15);
- Call all your friends and tell them what a genius you are and that they should check out your cool new randomized images…
Hope that made someone’s day…
Leave a Reply
You must be logged in to post a comment.
Thank you.
BOOKMARK
PRINT
EMAIL
