©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
(PECL imagick 2.0.0)
Imagick::getImageColors — Gets the number of unique colors in the image
Gets the number of unique colors in the image.
成功时返回 TRUE
。
[#1] holdoffhunger at gmail dot com [2012-04-06 22:54:59]
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
// Test File
// ---------------------------------------------
// Note: This file was just a picture of a 5x5 red square
// inside of a gigantic 521x512 black square, and
// then I did a bunch of random, yellow zigzags.
$file_to_grab_with_location = "test.bmp";
$imagick_type = new Imagick();
// Open File
// ---------------------------------------------
$file_handle_for_viewing_image_file = fopen($file_to_grab_with_location, 'a+');
$imagick_type->readImageFile($file_handle_for_viewing_image_file);
// Preset Information
// ---------------------------------------------
$frequency_list_of_values = array();
// Parse Pixels
// ---------------------------------------------
$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>");
// Parse Image Top-to-Bottom (Y-Variable)
// ---------------------------------------------
for($y = 0; $y < $image_resolution_height; $y++)
{
// Parse Image Left-to-Right (X-Variable)
// ---------------------------------------------
for($x = 0; $x < $image_resolution_width; $x++)
{
// Image Pixel Color
// ---------------------------------------------
$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);
// Set Key Value
// ---------------------------------------------
$key_value = $pixel_to_examine_color_value_red . " / " .
$pixel_to_examine_color_value_green . " / " .
$pixel_to_examine_color_value_blue ;
// Increment Array Entry for Color
// ---------------------------------------------
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 Out Array of Values
// ---------------------------------------------
print("<pre>");
print_r($frequency_list_of_values);
print("</pre>");
?>