Icy Phoenix

     
 

Random Background Changer

Random Background Changer

Article
Reply with quote    Download Post  
Post Random Background Changer 
 
Here is a very basic script which will allow you to select a random image from all the images in a folder to be used as random background (for example) for your website.

First of all let me provide an example for you:

Random Background

If you click the link above and then refresh the target page you will see that the image will change each time (one among all the images in the folder will be selected).

I use that script to change the header of my website, so each page will have a different header background:
Annunci Di Matrimonio

How to achieve that?

It is really easy.

  1. Create a folder on your website and put all images you want to rotate (this script is based upon JPG images, but it won't be tough to be adapted to other images).
  2. Create a php file (choose the name you like... random_bg.php for example) with this code in it:
    Code: [Download] [Hide] [Select]
    <?php
    $img_array
    = array('bg_01.jpg', 'bg_02.jpg', 'bg_03.jpg', 'bg_04.jpg', 'bg_05.jpg', 'bg_06.jpg', 'bg_07.jpg', 'bg_08.jpg', 'bg_09.jpg');

    $img_rnd = rand(0, sizeof($img_array) - 1);

    header('Content-type: image/jpg');
    header('Content-Disposition: filename=' . $img_array[$img_rnd]);
    readfile($img_array[$img_rnd]);
    ?>

  3. Make sure the array at the beginning of the file contains all the images you want to rotate:
    Code: [Download] [Hide] [Select]
    $img_array = array('bg_01.jpg', 'bg_02.jpg', 'bg_03.jpg', 'bg_04.jpg', 'bg_05.jpg', 'bg_06.jpg', 'bg_07.jpg', 'bg_08.jpg', 'bg_09.jpg');

  4. Just recall the php file from your browser to test it (this is just an example):
    Code: [Download] [Hide] [Select]
    http://www.yoursite.com/images/random/random_bg.php

  5. If it is working fine, you can then use this image in your CSS or HTML code to output a random BG where needed!


I hope you will find this script useful!



 
Mighty Gorgon - View user's profile Send private message  
Mighty Gorgon [ Tue 25 May, 2010 21:02 ]
Icy Phoenix is an open source project, you can show your appreciation and support future development by donating to the project.

Support us


Random Background Changer

Comments
Reply with quote    Download Post  
Post Re: Random Background Changer 
 
can you hear that funny clapping sound??? it 's me applauding     sh_06



 
Joshua203 - View user's profile Send private message  
Joshua203 [ Tue 25 May, 2010 21:26 ]
Reply with quote    Download Post  
Post Re: Random Background Changer 
 
Thanks MG, I think this is very usefull

 



 
TheSteffen - View user's profile Send private message  
TheSteffen [ Thu 03 Jun, 2010 09:39 ]
Reply with quote    Download Post  
Post Re: Random Background Changer 
 
Thanks so much for posting this. It would be useful for this project that I have been working on. By the way, is it possible to embed the php coding into an html document? Please let me know. Thanks again.



 
cheryl - View user's profile Send private message  
cheryl [ Fri 09 Sep, 2011 08:26 ]
Reply with quote    Download Post  
Post Re: Random Background Changer 
 
What do you mean?

This modification doesn't need that, because you can create the php file and then include the image where needed by just using html.



 
Mighty Gorgon - View user's profile Send private message  
Mighty Gorgon [ Tue 13 Sep, 2011 00:12 ]
Reply with quote    Download Post  
Post Re: Random Background Changer 
 
cheryl wrote: [View Post]
By the way, is it possible to embed the php coding into an html document? Please let me know. Thanks again.


If you mean embedding it directly into an html mime type - something.html etc the answer is no you can't.

You have to trick it by using more than one file. Usually a variable.php, something.php and something.html files.



 
mort - View user's profile Send private message  
mort [ Tue 13 Sep, 2011 02:22 ]
Reply with quote    Download Post  
Post Re: Random Background Changer 
 
I had some questions at phpbb.com about this and think the next quote from that thread may help people that want to use this as a header background changer

Joshua203 wrote: 
cyanman85 wrote: 
Where do i put the folder with the images?

Try styles/milky_way/theme/images

cyanman85 wrote: 
Do I not have to make a path to the images folder in the php code? (It does not describe this).

No

cyanman85 wrote: 
Where do I put the php file I generate?

In the same folder as the images

Then you open the stylesheet.css with an editor (a good one ...not notepad) and find something that looks like this:
Code: [Download] [Hide]
  1. #top_logo{ 
  2.     border-width: 0px 0px 0px 0px; 
  3.     margin: 0px 0px 0px 0px; 
  4.     padding: 0px 0px 0px 0px; 
  5.     background: url('./images/top_header.png') repeat; 
  6.     height: 150px; 


Replace the line:
Code: [Download] [Hide]
  1.     background: url('./images/top_header.png') repeat; 

With:
Code: [Download] [Hide]
  1.     background: url('./images/YOUR FOLDER/random_bg.php') no-repeat; 


and post back how it looks

*Note... I've just tested this on black_pearl so the codes may differ a bit
BTW ...always backup the files before you edit them!!!


Greetings,
...Joshua203



 
Joshua203 - View user's profile Send private message  
Joshua203 [ Tue 13 Sep, 2011 16:28 ]
Display posts from previous:    

HideWas this topic useful?

Post new topic  Reply to topic  Page 1 of 1