Php 从图像生成调色板

Php 从图像生成调色板,php,gd,color-profile,Php,Gd,Color Profile,只是为了好玩,我一直在研究如何使用GD库从图像创建调色板。到目前为止,我已经使用GD将用户上传的图像调整到合适的大小,以便在网页上显示 现在,我希望能够从图像中获得五种左右不同的颜色,代表图像中存在的颜色范围。一旦我这样做了,我想根据这些颜色生成一个互补的调色板,然后我可以用它给页面上的不同元素上色 任何关于如何找到初始调色板的帮助我都将不胜感激 编辑: 我找到了我自己的解决方案,您可以在下面看到。imagecolorat函数将为您提供像素处的颜色值,您可以使用该值扫描图像并创建颜色直方图: 这

只是为了好玩,我一直在研究如何使用GD库从图像创建调色板。到目前为止,我已经使用GD将用户上传的图像调整到合适的大小,以便在网页上显示

现在,我希望能够从图像中获得五种左右不同的颜色,代表图像中存在的颜色范围。一旦我这样做了,我想根据这些颜色生成一个互补的调色板,然后我可以用它给页面上的不同元素上色

任何关于如何找到初始调色板的帮助我都将不胜感激

编辑:
我找到了我自己的解决方案,您可以在下面看到。

imagecolorat函数将为您提供像素处的颜色值,您可以使用该值扫描图像并创建颜色直方图:

这可能会对您有所帮助

<?php
$im = ImageCreateFromJpeg($source_file);

$imgw = imagesx($im);
$imgh = imagesy($im);

// n = total number or pixels

$n = $imgw*$imgh;

$colors = array();

for ($i=0; $i<$imgw; $i++)
{
        for ($j=0; $j<$imgh; $j++)
        {

                $rgb = ImageColorAt($im, $i, $j);

                if (isset($colors[$rgb])) {
                    $colors[$rgb]++;
                }
                else {
                    $colors[$rgb] = 1;
                }

        }
}
asort($colors);
print_r($colors);

好吧,我花了几天时间摆弄,这就是我如何构建调色板的方法。这对我来说效果相当好,你可以改变调色板的大小,从图像中返回更多或更少的颜色

// The function takes in an image resource (the result from one
// of the GD imagecreate... functions) as well as a width and
// height for the size of colour palette you wish to create.
// This defaults to a 3x3, 9 block palette.
function build_palette($img_resource, $palette_w = 3, $palette_h = 3) {
    $width = imagesx($img_resource);
    $height = imagesy($img_resource);

    // Calculate the width and height of each palette block
    // based upon the size of the input image and the number
    // of blocks.
    $block_w = round($width / $palette_w);
    $block_h = round($height / $palette_h);

    for($y = 0; $y < $palette_h; $y++) {
        for($x = 0; $x < $palette_w; $x++) {
            // Calculate where to take an image sample from the soruce image.
            $block_start_x = ($x * $block_w);
            $block_start_y = ($y * $block_h);

            // Create a blank 1x1 image into which we will copy
            // the image sample.
            $block = imagecreatetruecolor(1, 1);

            imagecopyresampled($block, $img_resource, 0, 0, $block_start_x, $block_start_y, 1, 1, $block_w, $block_h);

            // Convert the block to a palette image of just one colour.
            imagetruecolortopalette($block, true, 1);


            // Find the RGB value of the block's colour and save it
            // to an array.
            $colour_index = imagecolorat($block, 0, 0);
            $rgb = imagecolorsforindex($block, $colour_index);

            $colour_array[$x][$y]['r'] = $rgb['red'];
            $colour_array[$x][$y]['g'] = $rgb['green'];
            $colour_array[$x][$y]['b'] = $rgb['blue'];

            imagedestroy($block);
        }
    }

    imagedestroy($img_resource);
    return $colour_array;
}
//该函数接收图像资源(来自一个
//GD imagecreate…函数)以及宽度和
//要创建的调色板大小的高度。
//这默认为3x3,9块调色板。
函数构建调色板($img\u资源,$palete\u w=3,$palete\u h=3){
$width=imagesx($img_资源);
$height=imagesy($img_资源);
//计算每个调色板块的宽度和高度
//基于输入图像的大小和数字
//一块块。
$block_w=圆形($width/$palete_w);
$block_h=圆形($height/$palete_h);
对于($y=0;$y<$palete_h;$y++){
对于($x=0;$x<$palete\u w;$x++){
//计算从soruce图像中获取图像样本的位置。
$block\u start\u x=($x*$block\u w);
$block\u start\u y=($y*$block\u h);
//创建一个空白的1x1图像,我们将复制到其中
//图像样本。
$block=ImageCreateTureColor(1,1);
imagecopyresampled($block,$img_resource,0,0,$block_start_x,$block_start_y,1,1,$block_w,$block_h);
//将块转换为仅一种颜色的调色板图像。
imagetruecolortopalette($block,true,1);
//找到块颜色的RGB值并保存它
//到一个数组。
$COLOR_index=imagecolorat($block,0,0);
$rgb=图像颜色索引($block,$color\u index);
$COLOR_数组[$x][$y]['r']=$rgb['red'];
$COLOR_阵列[$x][$y]['g']=$rgb['green'];
$COLOR_阵列[$x][$y]['b']=$rgb['blue'];
图像处理(块);
}
}
图像销毁(img_资源);
返回$COLOR_数组;
}