Need to rotate some images on a page? Want to call a script from your CSS to randomly rotate background images? This sucker can do it all … as long as you’re running PHP, anyway. This script will load an image at random from a certain directory (all images from the same directory as the script is located) each time the page is loaded/refreshed.
rotator script (will open in new window)
Wanna know? Here we go:
- Click on the link above. This will open the script as a text file in a new window.
- Copy and paste the script in your favorite HTML or text editor, and save as rotator.php
- Place the rotator.php file in the same directory as the images you want to rotate … ONLY THOSE IMAGES you want to rotate can be in the directory. Keep in mind the script will use any image it finds in its directory.
- make a call to the file just as you would any other image:
- On-page images: <img src="https://www.yoursiteurl.com/images/rotate.php" alt="" />
- CSS background: background:url(https://www.yoursiteurl.com/images/rotate.php) no-repeat bottom center; (or whatever positioning styles you need)
- Follow any other instructions that may be in the comments of the script, and you should be good to go.
It’s really pretty simple to use. You can even have this script in multiple directories with images used for various elements, and have tons of things rotating on your page(s) … though that may not be advisable for usability reasons – try to keep it simple.