【问题标题】:How can I tweak this function to allow dithering?如何调整此功能以允许抖动?
【发布时间】:2011-04-13 12:23:18
【问题描述】:

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

所以.. 精灵 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);
}

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

【问题讨论】:

    标签: php gd dithering


    【解决方案1】:

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

    imagetruecolortopalette($image, false, 255);
    

    到:

    imagetruecolortopalette($image, true, 255);
    

    【讨论】:

    • 这样做对非抖动精灵没有影响。将此参数添加到抖动的精灵会使它们变得有斑点。这无助于颜色转换。
    • 抖动的结果取决于源图像中使用的颜色数量和第三个参数中指定的数量。但是,PHP 手册指出 imagetruecolortopalette-function 有时“效果不如预期”...
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2021-05-15
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2017-04-24
    相关资源
    最近更新 更多