English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I currently have 7 background images as jpeg files. I would like a random picture to be displayed when the browser loads the page. Is this possible using PHP? Can someone provide the code? Also the page is named as index.html would I then need to change this to index.php?

2007-05-30 23:00:52 · 7 answers · asked by Siu02rk 3 in Computers & Internet Programming & Design

C.C.I.

I could not get the code working from the link that was provided

$path = 'headers/';
$dir = dir($path);
$files = array();
while (false !== ($item = $dir->read())) {
if ($item != '.' && $item != '..' && substr($item, -4) == '.jpg') {
$files[] = $item;
}
}
$dir->close();

$random_key = array_rand($files, 1);

header("Content-Type: text/css");
?>
div#header {
background-image: url("");
}

2007-05-31 02:14:32 · update #1

7 answers

Keep it simple and fast!

Name your images image bg1, bg2, bg3 etc (for my example they are stored in /images/ directory).

Add styles to your css sheet:

.bg1 {background: white url(/images/bg1.jpg) no-repeat center;}
.bg2 {background: white url(/images/bg2.jpg) no-repeat center;}

... etc (you need 7 of them in this case). Or (less useful) put the styles in the of every page which uses the random background image ( inside tags).

Add to the :

$randombg = rand (1, 7);
?>

In the apply the random background image to any element you choose:

">

This produces a very streamlined output eg:



This method takes virtually no time to process - and your source code will reveal nothing at all - just nice clean html , which means that you have a higher ratio of content to code, to keep the search engines happy!

CHANGING THE PAGE NAME?

KEEP your index.html file and ADD a new index.php file and use php or htaccess to set a permanent redirect to your new php page. Don't get rid of your index.html file - you will mess up your search engine ranking. Also, anyone who has bookmarked your home page will get an error message instead of being redirected to your new page.

Alternatively, use htaccess to parse html files as php files.

2007-06-01 05:18:05 · answer #1 · answered by jersjazz 2 · 0 0

I have the same question ;-)
I looked around and tested several PHP codes but they didn't result. I'll keep you updated.

Hope someone else contributes here as I need that code too.

NOTE: the images should be in a specific folder, and the PHP code should randomly show all pics in that folder. This way, you are not limited to 7, and can add and delete pics whenever you want.

Yes, you will have to change to index.php

-----
Update: the message above looks great, but it doesn't work for me. Is it because the directories are protected??

2007-05-30 23:51:33 · answer #2 · answered by king76 3 · 0 0

Hey I just picked up this random snippet


/*
* Name your images 1.jpg, 2.jpg etc.
*
*/

// Change this to the total number of images in the folder
$total = "11";

// Change to the type of files to use eg. .jpg or .gif
$file_type = ".jpg";

// Change to the location of the folder containing the images
$image_folder = "images/random";

// You do not need to edit below this line

$start = "1";

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo "\"$image_name\"";

?>

This code will probably be best in an header file or near the start of your php page. The end code would need to be changed a little bit but you should be able to figure this out.

Good Luck
Paul

2007-05-31 02:26:58 · answer #3 · answered by phobzy85 2 · 0 0

1. Have your images called, say: bkg1.jpg, bkg2.jpg etc... and located in the same directory as the calling page.
2. In the calling page:
$maxbkg = 7; // this is the max number of pictures
$i = rand(1,$maxbkg); // gets a random number from 1 to 7
$bkgpic = "bkg" . $i . ".jpg"; // build the bkg name (ie bkg3.jpg)
echo ("\r\n");
?>
(after the = it is ' followed by ", and before the end, it is " followed by ' . No space in between. The \r\n is not necessary, but makes the clode cleaner when you look at it in HTML)
3. The calling page must end with ".php", not "html"...
That's it!

2007-05-31 19:48:22 · answer #4 · answered by just "JR" 7 · 0 0

It is 'of course' possible, I have the code for it at home (I once used it for a website I designed) and I'll send it to you later today if I don't forget it. :-) Yes, you'll have to rename the .html file to .php.

2007-05-31 01:13:41 · answer #5 · answered by Anonymous · 0 0

Yes but it's not recommanded since it'll increase your page load time ... Make sure your images are 200k or less. here:

http://www.famouswolf.com/weblog/view/article/random-background-image-through-css-and-php/

2007-05-30 23:43:10 · answer #6 · answered by C.C.I. 2 · 1 0

this is there to purpose and get you to affix for an account. in case you click on the hyperlink that announces 'exchange history image' contained in the backside left hand nook, it takes you to the Google bills website the place it says 'sign in to customize your Google journey'

2016-10-06 09:06:48 · answer #7 · answered by fogleman 4 · 0 0

fedest.com, questions and answers