2011-12-14 70 views
1

如何检测图像有多少颜色,然后返回PHP中这些颜色的RGB或十六进制值?随着标准的文件格式... PNG,JPG,GIF等检测上传图像有多少颜色

样品图片: enter image description here

用于: 在线设计工具,它允许你上传图片的使用设计师。我试图拒绝超过10种颜色的所有图像(或可能会减少颜色?)。对于颜色少于10色的图像,我试图返回存在的每种颜色的十六进制或RGB值(出现的颜色越多,打印过程越昂贵)。

+0

你能给一个图像的例子吗?这样的代码的目的。 – 2011-12-14 19:26:56

回答

4
$gd = imagecreatefrompng($filename); 

$width = imagesx($gd); 
$height = imagesy($gd); 

$colors = array(); 

for($x = 0; $x < $width; $x++) 
{ 
    for($y = 0; $y < $height; $y++) 
    { 
     $color = imagecolorat($gd, $x, $y); 
     $hex = sprintf("0x%06x", $color); 

     if(!in_array($hex, $colors)) 
     { 
      $colors[] = $hex; 
     } 
    } 
} 
+1

它是该死的缓慢,但我张贴相同的解决方案:) – TimWolla 2011-12-14 19:35:55