<?php
/*

Author: Shelley Powers
Site: http://burningbird.net

Dynamically generating CSS entries, including loading background image and sampling for colors.

Licensed under GPL. Use at your own risk. Have fun.
*/
?>

<?php
// declare the output of the file as CSS
header('Content-type: text/css');
?>
#header {
<?php

$exts 
= array('jpeg');

//collect list of images in current (look) directory
//modification suggested by Kaf Oseo
$url = array();
if(
$handle opendir(dirname(__FILE__))) {
        while(
false !== ($image readdir($handle)))
                foreach(
$exts as $ext)
                        if(
strstr($image'.' $ext))
                                
$url[] = $image;
        
closedir($handle);
}

//generate a random number
srand((double)microtime() * 1000000);

// number of images
$ct count($url);

// random image number
$rn = (rand()%$ct);

// get the image name
$imgname trim($url[$rn]);

// create a working image 
$im ImageCreateFromJpeg($imgname);

// sample five points in the image, based on rule of thirds and center
$rgb = array();

$rgb[1] = ImageColorAt($im26756);
$rgb[2] = ImageColorAt($im53456);
$rgb[3] = ImageColorAt($im267112);
$rgb[4] = ImageColorAt($im534112); 
$rgb[5] = ImageColorAt($im40084);

// extract each value for r, g, b
$r = array();
$g = array();
$b = array();

for (
$i 1$i <= 5$i++) {
   
$r[$i] = ($rgb[$i] >> 16) & 0xFF;
   
$g[$i] = ($rgb[$i] >> 8) & 0xFF;
   
$b[$i] = $rgb[$i] & 0xFF;
   }

printf("background-image: URL('%s');\n"$imgname);

?>
           background-repeat: repeat-x;
           background-position: top left;
}

<?php
   printf
(".color1 { fill: rgb($r[1],$g[1],$b[1]); stroke: rgb($r[4],$g[4],$b[4]); }\n");
   
printf(".color2 { fill: rgb($r[2],$g[2],$b[2]); stroke: rgb($r[3],$g[3],$b[3]); }\n");
   
printf(".color3 { fill: rgb($r[3],$g[3],$b[3]); stroke: rgb($r[2],$g[2],$b[2]); }\n");
   
printf(".color4 { fill: rgb($r[4],$g[4],$b[4]); stroke: rgb($r[1],$g[1],$b[1]); }\n");
   
printf(".color5 { fill: rgb($r[5],$g[5],$b[5]); }\n");
 
   
printf("stop.begin { stop-color: rgb($r[1],$g[1],$b[1]); }\n");
   
printf("stop.middle   { stop-color: rgb($r[5],$g[5],$b[5]); }\n");
   
printf("stop.end { stop-color: rgb($r[4],$g[4],$b[4]); }\n"); 
?>

#footer {
<?php

printf
("background-image: URL('%s');\n"$imgname);
?>
           background-repeat: no-repeat;
}