我发现了一个很棒的功能来显示图像中颜色的调色板。我唯一的问题是我需要能够支持PNG和GIF文件。不知道这是否是一个简单的解决方案。感谢任何帮助或指导。调色板PHP功能(png&gif支持)
function colorPalette($imageFile, $numColors= 5, $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;
}
$imgData = file_get_contents($imageFile);
$img = @imagecreatefromstring($imgData);
unset($imgData);
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);
}
编辑
我改变了上面的代码,以反映我试图完成。
这是函数如何被用来
$palette = colorPalette('image_path_here', 5);
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";
信贷的功能放在这里:http://board.phpbuilder.com/showthread.php?10355107-How-to-get-color-palette-from-images-using-PHP
你能提供一个像jsfiddle这样的工作示例的链接吗?我真的很想看看它是如何工作的! – razzak 2013-03-11 20:00:52
@razzak发布了上面的工作代码。它几乎输出一个类似于dribbble网站的调色板。 – souporserious 2013-03-11 20:08:11
非常感谢分享,我通常使用kuller网站https://kuler.adobe.com/#create/fromanimage生成图像的颜色主题,现在使用此代码会更容易:)? – razzak 2013-03-11 20:24:35