Skip to main content

How to Rotate Your Web Page Background at Radom

How to Rotate Your Web Page Background at Random


Problem: I want my web page background image to change every time the page is loaded by picking an image at random.

The images in this solution are all prepared in advance. They are all named live#.jpg where "#" is a number between 1 and whatever the number of images you have.  Hence they are named:

  • live1.jpg
  • live2.jpg
  • live3.jpg
  • ........
The naming system simplifies the script that will be used to load the images.

The approach adopted is a three-step algorithm:
  1. Create an array of the file names for these image files in a certain folder. In my case, the folder is called "slides".
  2. Count the number of files. There are possibilities of counting sub-directories and . and .. if not careful. In this solution, we ensure no subdirectories are in the target folder and do a little arithmetic to eliminate the .. and .. folders.
  3. Generate a random number based on the number of files in the target folder and append this number to the base name of the image files then use this name in the CSS background property.

PHP Script Example

$path = "slides/"; //the relative oo absolute path to the image files
$files = scandir($path); //get list of files in the target folder
$size = count($files); //count the number of files (including . and ..)
$number=rand(1,$size); //generate a random number between 1 and $size

Generate Image File Name 

echo("background-image: url(slides/live");
echo($number);
echo(".jpg);");


This could be shortened to:
echo("background-image: url(slides/live".$number.".jpg);");

Background Tricks

"background-size: cover;" forces the image to cover the entire element. See demo . 
Some older browsers may not implement it properly. See this discussion.

The final page looks like this (minus other CSS and HTML code):

---------------------------------------------------------
<!DOCTYPE html><html><head><title>Background Image Rotator</title><style type="text/css">

 html { <?php $path = "slides/"; $files = scandir($path); $size = count($files); $number=rand(1,$size); /* echo("background-image: url(slides/live"); echo($number); echo(".jpg);"); */ echo("background-image: url(slides/live".$number.".jpg);");
?>
-webkit-background-size: cover; /* for older browsers using webkit engine */ -moz-background-size: cover; /* for older Mozilla Firefox */ -o-background-size: cover; /* for older Opera browser */ background-size: cover; /* for all modern browsers */ /* add your CSS ... */
</style>

</head>

<body>
<!-- add your web page content here ... -->

</body> 

</html>
---------------------------------------------------------



To save time processing files, there are ways to rename files. The easiest is to highlight all files in a folder with Ctrl + A then type the base name and press ENTER. Windows will rename the files using the base name (such "Word Doc 2016_31_10") and a number as follows:

Original List
wrn_3

Output
wrn_4

See this site for a more detailed description.

You can remove the parentheses using the following two Powershell commands:

dir | rename-item -NewName {$_.name -replace "\(",""}
dir | rename-item -NewName {$_.name -replace "\)",""}

To launch Powershell, browse to the target directory then open the File menu as follows:



As you fire away at your keyboard, remember the Messiah is coming and prepare. I guarantee it.

Be blessed today.


Comments

Popular posts from this blog

How to Install RIOT Plugin for GIMP

How to Install RIOT Plugin for GIMP::  Since this article was first written a stand-alone version of RIOT has been made available Download the plugin from   http://luci.criosweb.ro/riot/download/   Select the download link titled " RIOT as plug-in for other applications "    Launch the  Riot-plugin.exe  file by double clicking. The installer installs all the files including Riot.exe, Riot.dll, and FreeImage.dll in the location where you installed GIMP  If GIMP is open, close it and re-launch it. To optimise an image, open it then save with menu  File-&gt;Save for web with RIOT    You can set the target file size by selecting " Compress to size " button  on bottom right corner of optimised image pane and providing the size in kilobytes. Alternatively you can use a slider provided to allow you to change the quality while showing you a preview of the resulting image. Enjoy .       ...

XUBUNTU PANEL AND WINDOW BUTTONS HAVE DISAPEARED

 XUBUNTU PANELS Xubuntu can be quite troublesome when you shutdown and restart. Desktop panels may disappear as well as window minimize/maximise/close buttons.  The panel is equivalent to the Windows 10 Start menu that provides access to applications. TO MAKE THE PANEL REAPPEAR Open a terminal by right-clicking on the desktop then enter the following: xfwm4 --replace TO RECOVER THE PANEL Open a terminal by right-clicking on the desktop then enter the following: xfCE-panel --restart  That is all . As we say in Kiswahili: Mambo kwisha!

Installing NGINX with PHP and MySQL

My OS: Windows 7 Home Premuim 64bit Software Installed: NGINX 0.9.5 for Windows PHP: php-5.3.5-Win32-VC6-x86.zip MySQL: mysql-5.5.9-winx64.msi NGINX Installation Download from  http://nginx.org/en/download.html Unzip into C:\nginx Change the nginx.conf in C:\nginx\conf as per instructions at http://blog.siteroller.net/set-up-nginx-mysql-and-php-wemp-on-windows See my nginx.conf file  here . Note  that in this example, it is assumed that PHP is installed in C:\nginx\php (unzipped into this folder) and MySQl is installed in C:\Program Files\MySQL PHP Installation After downloading the zip version of PHP (V6, threadsafe, zip), unzip into C:\nginx\php\ Rename  php.ini-production  to  php.ini Make changes to this file as given in at  http://blog.siteroller.net/set-up-nginx-mysql-and-php-wemp-on-windows  but do not expect the line numbers given to match exactly. Activate the extensions  extension=php_...