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)
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];
}