Here's a nifty PHP based wallpaper switcher.

Requires a PC with the ability to display HTML files on the desktop, and make valid HTTP queries. Active Desktop fits this requirement nicely. Also requires a working PHP install, a webserver, the ability to write to whatever directory it's stored in, and a folder full of good wallpaper.

The script is simple enough. Point your browser at it after install, and there is a basic config page. Minimum timeout, maximum timeout, and path to images. All images must currently be local, though if someone feels so inclined to send me code that lets me point to external images AND local images, please do.

As well, I do not know how it functions on anything but Windows, though I can't see it working badly on *nix. It works in both IE and Mozilla, as it was tested in both. Please report any glitches in other browsers, a fix for the glitch would be excellent.

I DO NOT CLAIM THIS CODE TO BE FIT FOR ANY PURPOSE. USE IT AT YOUR OWN RISK.

For simplicity's sake, I hereby license this under the terms of the BSD License.

©2002 Will Marone

Excluding the section written by fancao0515@0451.com, though his sample is freely available in the PHP annotated documentation.


<?php
//function list_dir was copied wholesale from the php.net annotated reference. No sense in duplicating effort.
//Thanks to fancao0515@0451.com for the code.

//--------------- Function Listing ------------------//
srand((double)microtime()*1000000);
function list_dir($dirname){
if($dirname[strlen($dirname)-1]!='\\')
$dirname.='\\';
static $result_array=array();
$handle=opendir($dirname);
while ($file = readdir($handle))
{
if($file=='.'||$file=='..')
continue;
if(is_dir($dirname.$file))
list_dir($dirname.$file.'\\');
else
$result_array[]=$dirname.$file;
}
closedir($handle);
return $result_array;
}

function generate_file_list($root) {
$images = list_dir($root);
$php_list = fopen("filelist.txt","w+");
for ($i=0;$i<=count($images) - 1;$i++) {
fwrite($php_list,$images[$i].chr(13).chr(10));
}
fclose($php_list);
printf("<META HTTP-EQUIV=Refresh CONTENT=\"1; URL=wallpaper.php\">");
unset($opt);
unset($regen);
}
function select_image() {
if (file_exists("filelist.txt")) $list = file("filelist.txt");
printf("<IMG SRC=\"%s\">",
"file:///".rtrim($list[rand(0,count($list) - 1)]));
}

function write_options_file($lowtime, $hightime, $dir) {
$settings = fopen("settings.php","w+");
fwrite($settings, '<?php'.chr(13));
fwrite($settings, '$lower_limit = '.$lowtime.';'.chr(13));
fwrite($settings, '$upper_limit = '.$hightime.';'.chr(13));
addslashes($dir);
fwrite($settings, '$root_image_directory = '."\"".$dir."\";".chr(13));
fwrite($settings,'?>');
fclose($settings);
}
//--------------- Main Code Section ----------------//
if (isset($opt) and $opt == "setopt") { write_options_file($mininput,$maxinput,$targetdir); }
if (!file_exists("settings.php")) {
$opt=1;
$regen=1;
printf (" <HTML>
<HEAD>
<TITLE> First Time Setup </TITLE>
</HEAD>
<BODY bgcolor=white>
The settings for the wallpaper randomizer need to be set up, as well you may wish to adjust the size of the window you are using to display the images<br>
<br>
<form method=get action=wallpaper.php>
<input type=hidden name=opt value=\"setopt\"></input>
Minimum time between switch (in minutes):
<input type=text name=mininput size=4></input><br>
Maximum time between switch (in minutes):
<input type=text name=maxinput size=5></input><br>
<br>
Local directory that contains the image (root of the folder):
<input type=text name=targetdir size=100></input><br>
<br>
<button type=submit>Submit Changes</button>
</form>
</body>
</html>" );
} else { include "settings.php"; }
if (isset($regen) and $regen == "generate") { generate_file_list($root_image_directory); }
if(!file_exists("settings.php") && !file_exists("filelist.txt")) { /*without the settings file, we have no target dir. Thus we wait... */ }
elseif (!file_exists("filelist.txt")) {
$opt=1;
$regen=1;
printf(" <HTML>
<HEAD>
<TITLE> Generation of File List </TITLE>
</HEAD>
<BODY bgcolor=white><center>
The file list must be generated before a wallpaper can be displayed.
<form method=get action=wallpaper.php>
<input type=hidden name=regen value=\"generate\"></input>
<button type=submit>Click here to Generate</button>
</form> ");
}
if ((!isset($regen) && !isset($opt))) {
printf("<HTML>
<HEAD> ");
printf("<META HTTP-EQUIV=Refresh CONTENT=\"%d; URL=wallpaper.php\">",
rand($lower_limit * 60,$upper_limit *60));
printf("</HEAD>
<BODY bgcolor=black>
<center> ");
select_image();
printf("</BODY>
</HTML>");
}
?>

Log in or register to write something here or to contact authors.