我正在處理需要檢測圖像中混合顏色百分比的應用程序。 我該如何在PHP中做到這一點? 這是一個代碼可以獲取顏色的名稱(RGB),但沒有任何百分比的混合顏色。在php中獲取混合顏色的百分比
<?php
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);
}
// sample usage:
$palette = colorPalette('te.jpg', 10, 4);
echo "<table>\n";
foreach($palette as $color)
{
echo "<tr><td style='background-color:#$color;width:2em;'> </td><td>#$color</td></tr>\n";
}
echo "</table>\n";
?>
它可能不起作用。它不能顯示它的顏色。我如何爲每種顏色寫一個if?http://webthink.ir/go/iron.php – 2014-12-05 10:14:14
@MohammadJavanshah固定。 – blue112 2014-12-05 10:25:29
它不起作用。我改變了它,但沒有發生。 – 2014-12-05 10:32:08