1

我写了下面的函数。它将一种颜色切换为另一种颜色。进入的图片是具有各种颜色和白色背景的 css 精灵。

所以.. 精灵 1 可能是蓝色,精灵 2 可能是绿色。

该函数将运行两次以用所需的任何颜色替换蓝色 + 绿色。

/**
 * Changes the color of a graphic.
 * $settings = array (
 *  'icon'
 *  'new_icon'
 *  'old_color' = array
 *  'new_color' = array
 * );
*/
function updateIconColor($settings=array()) {
    // Create Image
    $image = imagecreatefrompng($settings['icon']);

    // Convert True color image to a palatte
    imagetruecolortopalette($image, false, 255);

    // Restore Alpha
    $white = imagecolorclosest($image, 255, 255, 255);
    imagecolortransparent($image, $white);

    // Find + Set color
    $index = imagecolorclosest($image, $settings['old_color'][0],$settings['old_color'][1],$settings['old_color'][2]);
    imagecolorset($image, $index, $settings['new_color'][0], $settings['new_color'][1], $settings['new_color'][2]);

    // Restore Alpha
    imageAlphaBlending($image, true);
    imageSaveAlpha($image, true);

    // Save
    imagepng($image, $settings['new_icon']); // save image as gif
    imagedestroy($image);
}

我需要允许对这些图像进行抖动。有没有办法可以修改此功能以处理抖动图像或添加抖动本身?

4

1 回答 1

1

要在调色板转换中添加抖动,请更改:

imagetruecolortopalette($image, false, 255);

至:

imagetruecolortopalette($image, true, 255);
于 2011-04-13T13:37:31.853 回答