获取照片中的颜色范围(PHP)

获取照片中的颜色范围(PHP),php,range,Php,Range,我想在图片中写一个红色范围的变量,如果大于98%,则使用它来显示消息。 这段代码可以得到每种颜色的百分比,但我需要一个变量来获得RGB的颜色范围。 您可以在这里看到此代码的示例。 点击 <?php function colorPalette($imageFile, $numColors, $granularity = 5) { $granularity = max(1, abs((int)$granularity)); $colors = array(); $s

我想在图片中写一个红色范围的变量,如果大于98%,则使用它来显示消息。 这段代码可以得到每种颜色的百分比,但我需要一个变量来获得RGB的颜色范围。 您可以在这里看到此代码的示例。 点击

    <?php

function colorPalette($imageFile, $numColors, $granularity = 5)
{
   $granularity = max(1, abs((int)$granularity));
   $colors = array();
   $size = @getimagesize($imageFile);
   if($size === false)
   {
      user_error("Unable to get image size data");
      return false;
   }
   $img = @imagecreatefromjpeg($imageFile);
   if(!$img)
   {
      user_error("Unable to open image file");
      return false;
   }
   for($x = 0; $x < $size[0]; $x += $granularity)
   {
      for($y = 0; $y < $size[1]; $y += $granularity)
      {
         $thisColor = imagecolorat($img, $x, $y);
         $rgb = imagecolorsforindex($img, $thisColor);
         $red = round(round(($rgb['red'] / 0x33)) * 0x33); 
         $green = round(round(($rgb['green'] / 0x33)) * 0x33); 
         $blue = round(round(($rgb['blue'] / 0x33)) * 0x33); 
         $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
         if(array_key_exists($thisRGB, $colors))
         {
            $colors[$thisRGB]++;
         }
         else
         {
            $colors[$thisRGB] = 1;
         }
      }
   }
   arsort($colors);
   return array_slice($colors, 0, $numColors, true);


}
// sample usage:
$palette = colorPalette('te.jpg', 10, 4);
echo "<table>\n";
$sum = array_sum($palette);
foreach($palette as $color => $count)
{
   echo "<tr><td style='background-color:#$color;width:2em;'>&nbsp;</td><td>#$color (".(($count / $sum) * 100).")</td></tr>\n";
}

echo "</table>\n"; 
?>
$caution="#ff0000 to #600000"
If $caution > 98% {
echo "Red area";}