Hi there What is the most convenient php script for a background image rotation? I want to be able to have my background image (800px X 600px) to rotate with different images on every page load or refreshing a page? Example: http://www.benharper.com/ Would anyone know how to accomplish this? Many thanks in advance CHEERS
Well something like this should do the trick : <?php $image[1]='http://www.yoursite/image.jpg'; $image[2]='http://www.yoursite/image2.jpg'; $image[3]='http://www.yoursite/image3.jpg'; $image[4]='http://www.yoursite/image4.jpg'; $image[5]='http://www.yoursite/image5.jpg'; $image[6]='http://www.yoursite/image6.jpg'; $randnum=rand(1,6); echo '<img scr="'.$image[$randnum].'">'; ?> PHP:
Here, I wrote a script for it. http://freebies.enspherant.com/rotating-image/ Actually, it tuens out the since all of the browsers I tried allow you to show an image with a .php extension and the proper content-type headers it was fairly easy to make a script that will rotate gif/jpg/png images automatically in any css style or html IMG tag. Turns out it's useful for more than just rotating backgrounds, you could rotate banners with it, too. The linked page shows a demo, you could even have several different random backgrounds in use on the same page.
Put this at the top of your page <?php $dir = 'path/to/images/'; $imgs = glob($dir.'*.jpg'); $img = '<img src="/'.$imgs[array_rand($imgs)].'" />'; ?> PHP: Then wherever you want the image just put <?php echo $img; ?> PHP: This script will randomly put an image with the .jpg extension from the directory path/to/images/ into an <img> tag you can use with just echo $img
<?php $dir = 'bg/'; $imgs = glob($dir.'*.jpg'); $img = '<img src="/'.$imgs[array_rand($imgs)].'" />'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Page1</title> <style type="text/css"> <!-- body { background-image: url(<?php echo $img; ?>); // I tried url(bg/<?php echo $img; ?>); too margin-top: 20px; } Code (markup): I did this but the background image doesn't appear. Am I doing something wrong? Thanks
Change this line $img = '<img src="/'.$imgs[array_rand($imgs)].'" />'; to $img = $imgs[array_rand($imgs)]; that should solve it
<?php /********************** * Image Rotater *********************** * Written by Joel Larson (Coded Caffeine) * http://thejoellarson.com/ * * Allows a person to add images with descriptions * and pick an image randomly from a list. **********************/ # Defined variables from paranoia. $images = array(); $output = ''; /*********************/ # Copy/paste as many as you need. # There are two shown for an example! (Make sure they are unique!) $images[] = array( 'path' => 'URL', 'alternate' => 'ALTERNATE TEXT', 'title' => 'DESCRIPTION' ); $images[] = array( 'path' => 'URL', 'alternate' => 'ALTERNATE TEXT', 'title' => 'DESCRIPTION' ); # Set the int for total images in the first array level. $total_images = count($images); # Using mt_rand because it's faster than rand(). $id = mt_rand(0, $total_images); /** * Set the output that's being generated. */ # If a path is defined.. if( ! empty($images[$id]['path'])) { # Create the image with path. $output = '<img src="'.$images[$id]['path'].'" '; # If the alt text is defined, add it. if( ! empty($images[$id]['alternate'])) $output .= 'alt="'.$images[$id]['alternate'].'" '; # If the title text is defined, add it. if( ! empty($images[$id]['title'])) $output .= 'title="" '; $output .= '/>'; } # Echo the result. echo $output; PHP:
Sweet! I tested all of the PHP codes, but couldn't get all of them to work properly for some reasons except for this code: <?php /********************** * Image Rotater *********************** * Written by Joel Larson (Coded Caffeine) * http://thejoellarson.com/ * * Allows a person to add images with descriptions * and pick an image randomly from a list. **********************/ # Defined variables from paranoia. $images = array(); $output = ''; /*********************/ # Copy/paste as many as you need. # There are two shown for an example! (Make sure they are unique!) $images[] = array( 'path' => 'URL', 'alternate' => 'ALTERNATE TEXT', 'title' => 'DESCRIPTION' ); $images[] = array( 'path' => 'URL', 'alternate' => 'ALTERNATE TEXT', 'title' => 'DESCRIPTION' ); # Set the int for total images in the first array level. $total_images = count($images); # Using mt_rand because it's faster than rand(). $id = mt_rand(0, $total_images); /** * Set the output that's being generated. */ # If a path is defined.. if( ! empty($images[$id]['path'])) { # Create the image with path. $output = '<img src="'.$images[$id]['path'].'" '; # If the alt text is defined, add it. if( ! empty($images[$id]['alternate'])) $output .= 'alt="'.$images[$id]['alternate'].'" '; # If the title text is defined, add it. if( ! empty($images[$id]['title'])) $output .= 'title="" '; $output .= '/>'; } # Echo the result. echo $output; ?> PHP: By the way, I forgot to mention that I was using XAMMP to test my PHP pages. Would that have any effect on the other PHP codes? I was thinking instead of typing out the URL path individually, is there a PHP code where can link a 'dir' folder with images and have them rotate randomly? If not, then I guess I can type out the path individually. Thanks heaps guys. CHEERS
The code I gave should work although might not on a windows installation of XAMPP due to the paths. Changing that should do it (note that it is for jpg files only)
My bad, I didn't know it randomly does the folder, all you have to do is put the url name of that the image in the PHP code. $images[] = array( 'path' => 'URL', 'alternate' => 'ALTERNATE TEXT', 'title' => 'DESCRIPTION' ); PHP:
Cool! I had a funny feeling that it might be the windows installation of XAMPP. It took a while to realize that it didn't work because of XAMPP. Oh well, at least I know now. Yep all my files are .jpg.
The following script uses the filename as the description. For example, "Golden_Gate_Bridge.jpg" will set the value of $image_caption to "Golden Gate Bridge". This way, all you have to do is to simply copy (and later delete if necessary) files to and from the images directory. You can change the title by simply changing the filename. <?php /*********************************************************************** * * Pick randndom image and display a title / alternate text based on * the filename. * * The filename can have spaces, and should have a jpg, png or gif * extension. Underscores also will be turend into spaces. * * Written by the Jedi Knight * * http://forums.digitalpoint.com/member.php?u=371818 * **********************************************************************/ $image_dir = 'images'; /* No need to change anything below */ $images = glob("$image_dir/*.jpg"); $images = array_merge($images, glob("$image_dir/*.png")); $images = array_merge($images, glob("$image_dir/*.gif")); $image = $images[array_rand($images)]; $image_caption = str_replace('_', ' ', preg_replace('/^.*[\/\\\\]([^\/\\\\]+)\.[^\/\\\\]+$/', '$1', $image)); /* Now you can use $image and $image_caption as necessary */ ?> PHP: For example, to use the random image as the body background, use the following, as someone else has suggested before: <style type="text/css"> <!-- body { background-image: url(<?php echo $image; ?>); margin-top: 20px; } --> </style> PHP: To show a description, simply print the value of $image_caption.