i want to find the dominant color in image, how can i do it ?
it would be great if i can get this in HEX code (exm: #eeeeee)
In regard to tkone answer, the $max is just a parameter showing density of the color at an image. I would change the code a bit to return the HEX color:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Empty Document</title>
</head>
<body>
<?php
error_reporting(0);
function rgb2hex($rgb) {
$hex = "#";
$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);
return $hex; // returns the hex value including the number sign (#)
}
$source_file = "image.jpg";
// histogram options
$maxheight = 300;
$barwidth = 2;
$im = ImageCreateFromJpeg($source_file);
$imgw = imagesx($im);
$imgh = imagesy($im);
// n = total number or pixels
$n = $imgw*$imgh;
$histo = array();
for ($i=0; $i<$imgw; $i++)
{
for ($j=0; $j<$imgh; $j++)
{
// get the rgb value for current pixel
$rgb = ImageColorAt($im, $i, $j);
//echo $rgb."<br>";
// extract each value for r, g, b
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
// get the Value from the RGB value
$V = round(($r + $g + $b) / 3);
//echo $V."<br>";
// add the point to the histogram
$histo[$V] += $V / $n;
$histo_color[$V] = rgb2hex([$r,$g,$b]);
}
}
// find the maximum in the histogram in order to display a normated graph
$max = 0;
for ($i=0; $i<255; $i++)
{
if ($histo[$i] > $max)
{
$max = $histo[$i];
}
}
echo "<div style='width: ".(256*$barwidth)."px; border: 1px solid'>";
for ($i=0; $i<255; $i++)
{
$val += $histo[$i];
$h = ( $histo[$i]/$max )*$maxheight;
echo "<img src=\"img.gif\" width=\"".$barwidth."\"
height=\"".$h."\" border=\"0\">";
}
echo "</div>";
$key = array_search ($max, $histo);
$col = $histo_color[$key];
?>
<p style="min-width:100px; min-height:100px; background-color:<?php echo $col?>;"></p>
<img src="<?php echo $source_file?>">
</body>
</html>
Also, it is worth mentioning that this is just the most 'repeated' color at the image that cannot absolutely considered 'dominant' color.
You give an image ressouce as argument.
Ex:
$im=imagecreatefromjpeg("path/to/image");
getDominantcolor($im);
NB: This function will little bit slow down your page since it will compare each color of the image to all other color to make the statistic. It returns rgb value of the dominant color as array
function getDominantcolor($im){
$width=imagesx($im);
$height=imagesy($im);
$colorArr=[];
$comparArr=[];
$colCount=0;
//fixed height
for ($j=0; $j < $height; $j++) {
//fetching color on widths
for ($i=0; $i < $height; $i++) {
$colorArr[]=imagecolorat ( $im ,$i ,$j );
}
}
//fixed height
for ($col=0; $col < count($colorArr); $col++) {
for ($cel=0; $cel < count($colorArr); $cel++) {
if($colorArr[$col]===$colorArr[$cel]){
$colCount+=1;
}
}
$comparArr[]=$colCount;
$colCount=0;
}
foreach ($comparArr as $key => $value) {
if($value===max($comparArr)){
$max_index=$key;
break;
}
}
$rgb=$colorArr[$max_index];
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
return ['r'=>$r,'g'=>$g,'b'=>$b];
}