doukuilian8365 2010-09-16 22:52
浏览 72
已采纳

PHP GD调色板颜色

In PHP GD how can you convert a truecolor image to a palette without losing any colors. With imagetruecolortopallete it doesn't work. I have a function that runs through all the colors and filters them (eg. grayscale). and It doesn't retain all colors, such as this picture of a Lamborghini-

Picture

alt text

This is my code

$im = imagecreatefrompng("lamborghini.png");
$degrees = 0;

$img = imagecreatetruecolor(imagesx($im), imagesy($im));
imagecopy($img, $im, 0, 0, 0, 0, imagesx($im), imagesy($im));
imagetruecolortopalette($img, true, 256);
$t = imagecolorstotal($img);
for ($i = 0; $i < $t; $i++) {
  $rgb = imagecolorsforindex($img, $i);
  $hsv =rgbtohsv($rgb['red'], $rgb['green'], $rgb['blue']);
  $h = $degrees;
  $s = $hsv['s'];
  $v = $hsv['v'];
  while ($h > 360) {$h -= 360;};
        $nrgb = hsvtorgb($h, $s, $v);
  imagecolorset($img, $i, $nrgb['r'], $nrgb['g'], $nrgb['b']);
}
imagecopy($im, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));

header('Content-type: image/png');

imagepng($im);
imagedestroy($im);

And it looks like this

alt text

You can see it loses colors. Is there any solution?

Also I don't think it has to do with my code but how imagetruecolortopalette outputs it

  • 写回答

3条回答 默认 最新

  • dpgvdfg321041670 2010-09-17 03:43
    关注

    Check the return on imagecolorstotal, you are always getting 256 colors as the return no matter how high you set the number of colors to be dithered to. PNG-8 & GIF formats only support palettes of up to 256 colors. So even if you can use more than 256 in a palette, you'd have to save it back as true color for anyone to be able to use it, thus making the whole conversion process a waste of time. In other words imagetruecolortopallete has an upper limit of 256 colors, you cannot go higher.

    Here's how you can do it in truecolor, though it's resource intensive. Maybe look into imagemagick if you want to do this more efficiently.

    $im = imagecreatefrompng("lamb.png");
    $img = imagecreatetruecolor(imagesx($im), imagesy($im));
    $degrees = 0;
    if ($degrees > 360) {$degrees = $degrees % 360 ;}
    
    foreach (range(0, imagesx($im) - 1) as $x ) {
        foreach (range(0, imagesy($im) - 1) as $y) {
            $rgb = imagecolorat($im, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            $hsv = rgbtohsv($r,$g,$b);
            $rgb = hsvtorgb($degrees, $hsv['s'], $hsv['v']);
            imagesetpixel($img, $x,$y,imagecolorallocate($img, $rgb['r'], $rgb['g'], $rgb['b']));
        }
    }
    
    imagepng($img, 'lamb2.png');
    

    Edit: Adding rgbtohsv & hsvtorgb functions. I did not write these functions.

    function rgbtohsv ($R, $G, $B) {
     // HSV Results:Number 0-1
    $HSL = array();
    
    $var_R = ($R / 255);
    $var_G = ($G / 255);
    $var_B = ($B / 255);
    
    $var_Min = min($var_R, $var_G, $var_B);
    $var_Max = max($var_R, $var_G, $var_B);
    $del_Max = $var_Max - $var_Min;
    
    $V = $var_Max;
    
    if ($del_Max == 0)
    {
    $H = 0;
    $S = 0;
    }
    else
    {
    $S = $del_Max / $var_Max;
    
    $del_R = ( ( ( $var_Max  - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
    $del_G = ( ( ( $var_Max  - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
    $del_B = ( ( ( $var_Max  - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
    
    if ($var_R == $var_Max) $H = $del_B - $del_G;
    else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
    else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;
    
    if ($H<0) $H++;
    if ($H>1) $H--;
    }
    
    $HSL['h'] = $H;
    $HSL['s'] = $S;
    $HSL['v'] = $V;
    
    return $HSL;
    }
    
    function hsvtorgb ($H, $S, $V) 
    {
    $RGB = array();
    
    if($S == 0)
    {
    $R = $G = $B = $V * 255;
    }
    else
    {
    $var_H = $H * 6;
    $var_i = floor( $var_H );
    $var_1 = $V * ( 1 - $S );
    $var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
    $var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );
    
    if ($var_i == 0) { $var_R = $V ; $var_G = $var_3 ; $var_B = $var_1 ; }
    else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $V ; $var_B = $var_1 ; }
    else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $V ; $var_B = $var_3 ; }
    else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $V ; }
    else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $V ; }
    else { $var_R = $V ; $var_G = $var_1 ; $var_B = $var_2 ; }
    
    $R = $var_R * 255;
    $G = $var_G * 255;
    $B = $var_B * 255;
    }
    
    $RGB['r'] = $R;
    $RGB['g'] = $G;
    $RGB['b'] = $B;
    
    return $RGB;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)