Dribbble.comの機能をイメージの主な色を検出して複製しようとしています。下の画像では、Dribbble.comのスクリーンショットを見ることができます。このスクリーンショットでは、画像の8つの主な色が左側に表示されています。ここに画像内の実際のページがありますhttp://dribbble.com/shots/528033-Fresh-Easy?list=followingPHPで画像内の主要な色を検出
私が必要とする色が得られたら、これをデータベースに保存して処理をすべてのページで実行する必要はありません負荷。
画像からこれらの色をどのように取得するかについての研究が行われた後、画像をピクセル単位で調べて、最も多く発生する色を保存するという人もいます。他の人にはそれ以上のことがあり、最も頻繁に存在する色を得ることは望ましい影響を与えないと言います。彼らは画像/色を量子化する必要があると言います(私はこの時点で失われています)。ドリブル下の画像で
は同じことを行いJavaScriptライブラリで以下のショット、そのページは私が見ることができるページのソースを表示するここhttp://lokeshdhakar.com/projects/color-thief/
閲覧することができquantize.js
というJavaScriptファイルがありますし、結果は本当に良いです。だから私は、私は色を返し、PHPで画像にカウントされます。この機能を発見したJavaScriptライブラリがないこと何が、PHPとGD/ImageMagickの
で行うことができるように期待していますしかし、結果は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 –
@aSeptik私がすでに投稿したコードと同じように見える – JasonDavis
あなたは試してみましたか? ''画像からカラーパレットを取得する '' phpのためにgoogleを検索する "'私は多くの結果を得ました。ちょっと尋ねる... –