我試圖複製Dribbble.com檢測圖像中主要顏色的功能。在下面的圖片中,您可以看到來自Dribbble.com的屏幕截圖,其中顯示了左側圖像中的8種主要顏色。這裏是圖像中的實際頁面http://dribbble.com/shots/528033-Fresh-Easy?list=following使用PHP檢測圖像中的主要顏色
我需要能夠在PHP中做到這一點,一旦我得到了我需要的顏色,我將它們保存到數據庫,因此無需在每個頁面上運行處理加載。
經過一些關於如何從圖像中獲取這些顏色的研究之後,有人說你只是逐個像素地檢查一個圖像,然後保存發生最多的顏色。其他人表示,還有更多,並且獲得最常見的顏色不會產生理想的效果。他們說你需要對圖像/顏色進行量化(我現在已經迷失了)。
在下面的運球鏡頭下方是一個JavaScript庫,做同樣的事情,該頁面可以在這裏http://lokeshdhakar.com/projects/color-thief/
查看查看該網頁,我可以看到有一個名爲quantize.js
JavaScript文件的源圖像和結果非常好。所以,我希望能夠做什麼的Javascript庫做,但使用PHP和GD/ImageMagick的
我找到了這個功能,將與PHP返回的顏色和計數的圖像但結果是從JavaScript版本以上的不同和運球結果
/**
* Returns the colors of the image in an array, ordered in descending order, where the keys are the colors, and the values are the count of the color.
*
* @return array
*/
function Get_Color()
{
if (isset($this->image))
{
$PREVIEW_WIDTH = 150; //WE HAVE TO RESIZE THE IMAGE, BECAUSE WE ONLY NEED THE MOST SIGNIFICANT COLORS.
$PREVIEW_HEIGHT = 150;
$size = GetImageSize($this->image);
$scale=1;
if ($size[0]>0)
$scale = min($PREVIEW_WIDTH/$size[0], $PREVIEW_HEIGHT/$size[1]);
if ($scale < 1)
{
$width = floor($scale*$size[0]);
$height = floor($scale*$size[1]);
}
else
{
$width = $size[0];
$height = $size[1];
}
$image_resized = imagecreatetruecolor($width, $height);
if ($size[2]==1)
$image_orig=imagecreatefromgif($this->image);
if ($size[2]==2)
$image_orig=imagecreatefromjpeg($this->image);
if ($size[2]==3)
$image_orig=imagecreatefrompng($this->image);
imagecopyresampled($image_resized, $image_orig, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); //WE NEED NEAREST NEIGHBOR RESIZING, BECAUSE IT DOESN'T ALTER THE COLORS
$im = $image_resized;
$imgWidth = imagesx($im);
$imgHeight = imagesy($im);
for ($y=0; $y < $imgHeight; $y++)
{
for ($x=0; $x < $imgWidth; $x++)
{
$index = imagecolorat($im,$x,$y);
$Colors = imagecolorsforindex($im,$index);
$Colors['red']=intval((($Colors['red'])+15)/32)*32; //ROUND THE COLORS, TO REDUCE THE NUMBER OF COLORS, SO THE WON'T BE ANY NEARLY DUPLICATE COLORS!
$Colors['green']=intval((($Colors['green'])+15)/32)*32;
$Colors['blue']=intval((($Colors['blue'])+15)/32)*32;
if ($Colors['red']>=256)
$Colors['red']=240;
if ($Colors['green']>=256)
$Colors['green']=240;
if ($Colors['blue']>=256)
$Colors['blue']=240;
$hexarray[]=substr("0".dechex($Colors['red']),-2).substr("0".dechex($Colors['green']),-2).substr("0".dechex($Colors['blue']),-2);
}
}
$hexarray=array_count_values($hexarray);
natsort($hexarray);
$hexarray=array_reverse($hexarray,true);
return $hexarray;
}
else die("You must enter a filename! (\$image parameter)");
}
於是我問,如果有人知道我該怎麼做這樣的任務用PHP?可能已經存在的東西已知,或者有任何提示讓我更近一步做到這一點,將不勝感激
http://stackoverflow.com/questions/3468500/detect-overall-average-color-of-the-picture – 2012-04-24 00:43:27
@aSeptik它看起來像我已經發布的代碼一樣 – JasonDavis 2012-04-24 00:50:52
您是否嘗試過搜索谷歌的「」從圖像獲取調色板「'我創建了很多結果;只是問... – 2012-04-24 00:52:49