O exemplo anterior apresentou números fracionados nas chaves da variável $frequency_list_of_values
Array
(
[1 / 0 / 1] => 157295
[0.83529411764706 / 0.87843137254902 / 0.82352941176471] => 1
[0.80392156862745 / 0.84705882352941 / 0.7843137254902] => 2
[0.8156862745098 / 0.84705882352941 / 0.78823529411765] => 1
...
Então eu fiz uma alteração que melhora o resultado para quem esta buscando saber quais as cores dominantes na imagem, mostrando a chave com o valor rgb exato
<?php
$file_to_grab_with_location = $imagepath; $imagick_type = new \Imagick();
$file_handle_for_viewing_image_file = fopen($file_to_grab_with_location, 'a+');
$imagick_type->readImageFile($file_handle_for_viewing_image_file);
$frequency_list_of_values = array();
$image_resolution_width = $imagick_type->getImageWidth();
$image_resolution_height = $imagick_type->getImageHeight();
print("圖片解析度:寬度 - $image_resolution_width / 高度 - $image_resolution_height<br><br>");
for($y = 0; $y < $image_resolution_height; $y++)
{
for($x = 0; $x < $image_resolution_width; $x++)
{
$pixel_to_examine = $imagick_type->getImagePixelColor($x,$y);
if(isset($frequency_list_of_values[$pixel_to_examine->getColorAsString()]) == TRUE)
{
$temp = $frequency_list_of_values[$pixel_to_examine->getColorAsString()];
$temp++;
$frequency_list_of_values[$pixel_to_examine->getColorAsString()] = $temp;
}
else
{
$frequency_list_of_values[$pixel_to_examine->getColorAsString()] = 1;
}
}
arsort($frequency_list_of_values);
}
print("<pre>");
print_r($frequency_list_of_values);
print("</pre>");
?>
結果
圖片解析度:寬度 - 1965 / 高度 - 998
Array
(
[srgba(255,0,255,0)] => 451654
[srgb(54,52,53)] => 281069
[srgb(177,51,54)] => 130449
[srgb(236,50,55)] => 64001
...
對於非常大的圖片,此方法會很耗時,因為它會逐個像素進行分析。