If you refresh our page or click our top menu you will see that our background image is changing randomly. This is not a big programming issue or bid deal, just some simple tricks we use. Now we are going to discuss about this with more details.

First you need to decide in which part you are going to implement this trick. You can implement it in anywhere in your template like body, left sidebar, right sidebar, footer and others part you want. For this tutorial we are going to use “body” element because we are going to change full website background image.

First you need to create a folder where you will store your all background images. You can create this folder anywhere under your site directory but create this under your image folder or template image folder for better maintenance. Now rename this folder as you want we are going to use “backgrounds”.

Upload your images into this folder and name it like: bg-1.jpg, bg-2.jpg, bg-3.jpg etc. Now we are going to generate a random number using PHP

$currentNum= rand(1,5);  // this code will generate a random number 1-5. 1 is lowest and 5 is highest
This code is very simple and self explained isn’t it?

I am going to explain 2 techniques to load background image.

Technique#1

Load image directly under body element. If you know a little about html then you probably know that it’s easy to embed any image using “style” tag. Now we are going to use it:

<body style="background-image: url('/images/backgrounds/bg-<?php echo $currentNum;?>.jpg');">

Technique#2

Using this same php code you can load a random ID or Class inside you body element and u can load background image form a css file.

<body id="<?php echo "bg-$currentNum"; ?>" >

This code will generate random id for body so you can easily trigger this id in ur css and change your background images. Personally i prefer this technique because you have more control over your design. Like you want to change your Top Menu color for bg-2 so you can change this things easily in this technique.

More Thought

Performance

Technique#1 is faster then Technique#2. I test this in my Core2Due 2.2ghz with 3GB RAM in Windows pc ans observe Technique#1 is more faster then Technique#2 in PHP microtime() benchmark.

Control over Design

Technique#2 is more flexible then Technique#1. Whenever you are generating a ID for your body element you have full control over your template with this ID. So not only background you can change anything with your background image.

2 comments

  • Comment Link car insurance quotes Monday, 17 May 2010 16:06 posted by car insurance quotes

    Thank you for this great tutorial.

    This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  • Comment Link SDP Wednesday, 21 April 2010 12:28 posted by SDP

    Do you know how to change pictures not randomly but according to sections? For example, home page got image with one man, when you click sports section it changes to a picture with a different man. The image is a header
    Thank you

    This e-mail address is being protected from spambots. You need JavaScript enabled to view it

Add comment