I've had difficulty getting this function to return the number of unique colors. Also, I wanted to be able to have an array with each $key value being the RGB of the pixel, and each $value value being the number of times that pixel occurs. Basically, a frequency list. For example, you would have "1 / 0 / 0" for red as a $key value, and "25" for the number of times that pixel color was in the image. So, I wrote some code to do that, using a combination of readImageFile, getImageWidth, getImageHeight, getImagePixelColor, and a simple x/y parser, like so :
<?php
$file_to_grab_with_location = "test.bmp";
$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_width / Height - $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);
$pixel_to_examine_color_value_red = $pixel_to_examine->getColorValue(imagick::COLOR_RED);
$pixel_to_examine_color_value_green = $pixel_to_examine->getColorValue(imagick::COLOR_GREEN);
$pixel_to_examine_color_value_blue = $pixel_to_examine->getColorValue(imagick::COLOR_BLUE);
$key_value = $pixel_to_examine_color_value_red . " / " .
$pixel_to_examine_color_value_green . " / " .
$pixel_to_examine_color_value_blue ;
if(isset($frequency_list_of_values[$key_value]) == TRUE)
{
$temp = $frequency_list_of_values[$key_value];
$temp++;
$frequency_list_of_values[$key_value] = $temp;
}
else
{
$frequency_list_of_values[$key_value] = 1;
}
}
}
print("<pre>");
print_r($frequency_list_of_values);
print("</pre>");
?>