<?php

// photographs.php
// author: shelley powers
// optimization; ash searle
//
// photographs.php samples colors from a photo and then generates 
// CSS entries from the sampled colors
// Thanks to ash searle for optimizing the application to 
// output all CSS entries at once at the end

function rgbhex($red$green$blue)
{
    return 
sprintf('%02X%02X%02X'$red$green$blue);
}

$exts = array('jpeg');

//collect list of images in current (look) directory
$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);

$height imagesy($im);
$top $height 135;
$width imagesx($im);

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

$topy round($height 3);
$bottomy round(($height 3) * 2);
$leftx round($width 3);
$rightx round(($width 3) * 2);
$centery round($height 2);
$centerx round($width 2);

$rgb[1] = imagecolorat($im$leftx$topy);
$rgb[2] = imagecolorat($im$rightx$topy);
$rgb[3] = imagecolorat($im,  $leftx$bottomy);
$rgb[4] = imagecolorat($im,  $rightx$bottomy);
$rgb[5] = imagecolorat($im$centerx$centery);

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

$ct 0$val 5000;
// process points
for ($i 1$i <= 5$i++) {
    
$r[$i] = ($rgb[$i] >> 16) & 0xFF;
    
$g[$i] = ($rgb[$i] >> 8) & 0xFF;
    
$b[$i] = $rgb[$i] & 0xFF;

    
// find darkest color
    
$tmp $r[$i] + $g[$i] + $b[$i];
    if (
$tmp $val) {
    
$val $tmp;
    
$ct $i;
    }

    
$hex[$i] = rgbhex($r[$i],$g[$i],$b[$i]);
}

// declare the output of the file as CSS
header('Content-type: text/css');

echo <<<EOF
#header {
    height: 
{$height}px;
    background: url(
{$imgname}) no-repeat bottom left;
}
.color1 { fill: #
{$hex[1]}; }
.color2 { fill: #
{$hex[2]}; }
.color3 { fill: #
{$hex[3]}; }
.color4 { fill: #
{$hex[4]}; }
.color5 { fill: #
{$hex[5]}; }

.block1 { stroke: #
{$hex[1]}; }
.block2 { stroke: #
{$hex[2]}; }
.block3 { stroke: #
{$hex[3]}; }
.block4 { stroke: #
{$hex[4]}; }
.block5 { stroke: #
{$hex[5]}; }

#stripe1 { background-color: #
{$hex[1]}; }
#stripe2 { background-color: #
{$hex[3]}; }
#stripe3 { background-color: #
{$hex[5]}; }
#stripe4 { background-color: #
{$hex[2]}; }
#stripe5 { background-color: #
{$hex[4]}; }

body { background-color: #
{$hex[3]}; }
#inner { border-left: 1px solid #
{$hex[3]}; border-right: 1px solid #{$hex[3]}; }
#topmeta { background-color: #
{$hex[$ct]}; }

h1#shadow { color: #
{$hex[$ct]}; }

stop.begin  { stop-color: #
{$hex[1]}; }
stop.middle { stop-color: #
{$hex[5]}; }
stop.end    { stop-color: #
{$hex[4]}; }
.nameExpanded, .nameCollapsed { background-color: #
{$hex[$ct]}; }
.column-post h2, .column-post h2 a, .firstpost, .firstpost a { color: #
{$hex[$ct]}; }
rect#toprect { y: 
{$top}px; }
EOF;