2009-03-24
This returns the average colour of an image in PHP using GD libraries and then, assuming you ahve no sense of design, you could use this to set the background colour of a page. This code can be slotted directly into the ImageHandler class also on this site
- public function getAverageColour(){
-
$colours = array('red'=>0,'green'=>0,'blue'=>0);
-
$count = 0;
-
$rgbstring = null;
-
-
for ($i=0;$iimage);$i++){
-
for ($j=0;$jimage);$j++){
-
$rgb = imagecolorat($this->image, $i, $j);
-
$ici = imagecolorsforindex($this->image, $rgb);
-
//print_r($ici);
-
foreach($colours as $key => $value){
-
$colours[$key] = $colours[$key] + $ici[$key];
-
}
-
$count++;
-
}
-
}
-
foreach($colours as $key => $value){
-
$colours[$key] = dechex(intval($colours[$key] / $count));
-
$rgbstring = $rgbstring . sprintf("%02s", $colours[$key]);
-
}
-
return $rgbstring;
-
}