2013-02-25 64 views
1

到目前爲止,我已經找到了關於如何抓取圖像中每個像素的所有RGB值的示例,但我希望能夠分解圖像併爲我提供簡化的調色板。使用PHP從圖像中獲取簡化的調色板?

有沒有辦法使用imagetruecolortopalette以某種方式吐出減少的調色板顏色,或將圖像分解成25 x 25塊,然後獲取該塊的平均值?

也許有一個替代我失蹤?我基本上只是想能夠在圖像中找到最常見的顏色。

在此先感謝。

回答

1

嗯,我已經爲客戶創建了類似的東西。截圖低於

enter image description here

完整的代碼如下

$microTime = microtime(true); 


function textColor($R1, $G1, $B1) { 

    $a = (($R1 * 299) + ($G1 * 587) + ($B1 * 114))/1000; 
    if ($a < 128) 
     return 'white'; 
    else 
     return 'black'; 
} 

function rgb2html($r, $g = -1, $b = -1) { 
    $hex = "#"; 
    $hex.= str_pad(dechex($r), 2, "0", STR_PAD_LEFT); 
    $hex.= str_pad(dechex($g), 2, "0", STR_PAD_LEFT); 
    $hex.= str_pad(dechex($b), 2, "0", STR_PAD_LEFT); 

    return $hex; 

    if (is_array($r) && sizeof($r) == 3) 
     list($r, $g, $b) = $r; 

    $r = intval($r); 
    $g = intval($g); 
    $b = intval($b); 

    $r = dechex($r < 0 ? 0 : ($r > 255 ? 255 : $r)); 
    $g = dechex($g < 0 ? 0 : ($g > 255 ? 255 : $g)); 
    $b = dechex($b < 0 ? 0 : ($b > 255 ? 255 : $b)); 

    $color = (strlen($r) < 2 ? '0' : '') . $r; 
    $color .= (strlen($g) < 2 ? '0' : '') . $g; 
    $color .= (strlen($b) < 2 ? '0' : '') . $b; 
    return '#' . $color; 
} 

function colorPalette($imageFile, $colorJump, $granularity = 5) { 

    $granularity = max(1, abs((int) $granularity)); 
    $colors = array(); 
    $ratio = array(); 
    $wastageCount = array(); 
    $occurrenceSCount = array(); 
    $occurrenceMCount = array(); 


    $size = @getimagesize($imageFile); 



    if ($size === false) { 
     return false; 
    } 
    $img = @imagecreatefromstring(file_get_contents($imageFile)); 

    if (!$img) { 
     user_error("Unable to open image file"); 
     return false; 
    } 
    for ($y = 0; $y < $size[1]; $y += $granularity) { 
     $lastColor = NULL; 
     $lastX = -1; 

     for ($x = 0; $x < $size[0]; $x += $granularity) { 
      $thisColor = imagecolorat($img, $x, $y); 
      $rgb = imagecolorsforindex($img, $thisColor); 

      $red = round(round(($rgb['red']/$colorJump)) * $colorJump); 
      $green = round(round(($rgb['green']/$colorJump)) * $colorJump); 
      $blue = round(round(($rgb['blue']/$colorJump)) * $colorJump); 

      $thisRGB = $red . ',' . $green . ',' . $blue; 


      if ($lastColor != $thisRGB) { 

       if (array_key_exists($thisRGB, $wastageCount)) { 
        $wastageCount[$thisRGB]++; 
       } else { 
        $wastageCount[$thisRGB] = 1; 
       } 

       if ($lastX + 1 == $x) { 
        if (array_key_exists($lastColor, $occurrenceSCount)) { 
         $occurrenceSCount[$lastColor]++; 
        } else { 
         $occurrenceSCount[$lastColor] = 1; 
        } 
       } 


       if ($lastX + 1 != $x) { 
        if (array_key_exists($lastColor, $occurrenceMCount)) { 
         $occurrenceMCount[$lastColor]++; 
        } else { 
         $occurrenceMCount[$lastColor] = 1; 
        } 
       } 

       $lastColor = $thisRGB; 
       $lastX = $x; 
      } 


      if (array_key_exists($thisRGB, $colors)) { 
       $colors[$thisRGB]++; 
      } else { 
       $colors[$thisRGB] = 1; 
      } 
     } 
    } 

    $totalPixels = array_sum($colors); 
    foreach ($colors as $k => $v) { 
     $ratio[$k] = round(($v/$totalPixels) * 100, 2); 
    } 

    return array($ratio, $wastageCount, $colors, $occurrenceSCount, $occurrenceMCount); 
} 

使用

$colorJump = 1; 
    $pixelJump = 1; 
    $paletteR = colorPalette($dbImgFile_dir, $colorJump, $pixelJump); 
    $palette = $paletteR[0]; 
    $wastage = $paletteR[1]; 
    $colorsFound = $paletteR[2]; 
    $occSArray = $paletteR[3]; 
    $occMArray = $paletteR[4]; 
    $totalPixels = array_sum($colorsFound); 

    $totalTime = abs(microtime(true) - $microTime); 

的循環周圍是比較複雜的,因爲我得從托盤數據庫,並與他們的顏色相匹配,並使用模板分析器是完全自定義的代碼,並不會幫助你。

僅有忽略從該所需的砝碼柱,單事件和多次出現的計算中,如果有一個單一的像素,例如RED, RED, BLUE, RED, RED這將是1個單發生和2多次出現

相關問題