Random Background Changer »  Show posts from    to     

Icy Phoenix


Free PHP Scripts - Random Background Changer



Mighty Gorgon [ Tue 25 May, 2010 21:02 ]
Post subject: 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: [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: [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: [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!


Joshua203 [ Tue 25 May, 2010 21:26 ]
Post subject: Re: Random Background Changer
can you hear that funny clapping sound??? it 's me applauding sh_06


TheSteffen [ Thu 03 Jun, 2010 09:39 ]
Post subject: Re: Random Background Changer
Thanks MG, I think this is very usefull



cheryl [ Fri 09 Sep, 2011 08:26 ]
Post subject: 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.


Mighty Gorgon [ Tue 13 Sep, 2011 00:12 ]
Post subject: 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.


mort [ Tue 13 Sep, 2011 02:22 ]
Post subject: 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.


Joshua203 [ Tue 13 Sep, 2011 16:28 ]
Post subject: 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: [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: [Hide]
  1. background: url('./images/top_header.png') repeat; 

With:
Code: [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




Powered by Icy Phoenix