0

I use the following code, to detect image colors:

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(array_keys($colors), 0, $numColors);
}

This code has already been introduced here
Detect "overall average" color of the picture
But, this code not working on image with resolution 2500px larger !
What the problem ?
Thanks a lot

Community
  • 1
  • 1
maxoya
  • 53
  • 1
  • 9

2 Answers2

2

You could potentially be creating an array of up to 16.7 million elements, assuming the image is large enough and contains every possible color. Even a 2500x2500 image can have 6.25 million possible unique colors.

A simpler workaround is to resize the image down to a single 1x1 pixel, and reading out that pixel's color..

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

You can divide the image and then check it , I think it will work.