Forum Moderators: coopster

Message Too Old, No Replies

Free Random Image Script

         

andrewsmd

7:26 pm on Nov 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had to generate a random image script for a client. I thought I would donate to the world and post it for all here. Note I didn't put in any debugging but I know it works. So the only way it would crash is if you change one of the beginning variables to an invalid value.

Here is the php
<?php
//this php creates a javascript block
//that dynamically updates the images.
//it was create by michael andrews on
//09-08-09 free to use and/or modify
//I don't even really care if you reference
//me for doing it

//these are all the things you need to edit
//don't touch anything outside of this box that
//isn't HTML

//--------------------------------------------------------------
// ¦
//! ¦
//set this to the time in seconds that you would ¦
//like the first picture to stay on the screen ¦
//! ¦
$initialTime = 9; // ¦
// ¦
//! ¦
//set this to the time in seconds that you would ¦
//like the rest of the pictures to stay on the screen ¦
//! ¦
$time = 5; // ¦
// ¦
//! ¦
//set this to the height and width you want the images ¦
//! ¦
$width = 412; // ¦
$height = 275; // ¦
//! ¦
//set this to folder where your images are ¦
//make sure you dont put a / on the end of your folder ¦
//it should read path/to/images NOT path/to/images/ ¦
//within images you can put in as many images as you want ¦
//type does not matter either just a valid image ¦
//! ¦
$folder = "Rotate"; // ¦
//! ¦
//set this to the image you want to be shown first ¦
//only set it to the name of the image and not the path ¦
//the path is defined in the previous line ¦
//! ¦
$startImage = "news.jpg"; // ¦
//--------------------------------------------------------------


$time = $time * 1000;
$initialTime = $initialTime * 1000;
$browserInfo = $_SERVER['HTTP_USER_AGENT'];

//echo the first static chunck of the JS
echo("<script language=\"JavaScript1.2\">
/*
Rotating image or text(You can use for changing banners)
Author: Narayan Chand Thakur
Source: [ncthakur.itgo.com...]
This may be used freely as long as this message is intact.
*/
<!--

//you may add your image file or text below
var item=new Array();");

//this is where we add the pictures dynamically to the array
//an array of all of the files in the folder
$path = glob($folder."/*");

//randomize the array
shuffle($path);

foreach($path as $i){

if($i != $path."/".$startImage){

if(stripos($browserInfo, "MSIE") !== false){
echo("item.push('{$i}');");
}//if
else{
//echo("item.push(\"<a href='#'><img src='{$i}' width='512' height='342' border='0'></a>\");");
echo("item.push(\"<a href='#'><img src='{$i}' width='{$width}' height='{$height}' border='0'></a>\");");
}

}//if

}//foreach

echo("var current=0;

var ns6=document.getElementById&&!document.all;
function changeItem(){

if(document.layers){
document.layer1.document.write(item[current]);
document.layer1.document.close();
}

if(ns6)document.getElementById(\"rotatingImage\").innerHTML=item[current]

{
if(document.all){
document.image1.src=item[current];
}
}
if (current==5) current=0
else current++;
setTimeout(\"changeItem()\",$time);
}

function init(){
setTimeout(\"changeItem()\", $initialTime);
}

window.onload=function() { init(); };

//-->
</script>");

?>

And here is the html/php you need to use it
<div id="rotatingImage"><img name="image1" src='Rotate/news.jpg' width='<?php echo($width); ?>' height='<?php echo($height); ?>' border='0'></a></div>

Enjoy and let me know if anyone has any problems. Just my way of giving back to all this site has given me.

andrewsmd

7:27 pm on Nov 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I forgot that this site looses all blank white space. If you want nice and neat formatted code send me a sticky mail.