如何调整此功能以允许抖动?
问题描述:
我写了下面的函数。它将一种颜色换成另一种颜色。进入的图片是具有各种颜色和白色背景的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);
}
我需要允许抖动这些图像。有没有一种方法可以修改这个函数来处理抖动图像或添加抖动本身?
答
要添加的色调转换抖动,变化:
imagetruecolortopalette($image, false, 255);
到:
imagetruecolortopalette($image, true, 255);
这样做有没有抖动精灵没有影响。将这个参数添加到抖动的小精灵中会使它们变得非常小巧。这不会帮助颜色转换。 – 2011-04-13 14:57:41
抖动的结果取决于源图像中使用的颜色数量和第三个参数中指定的数量。然而,PHP手册指出,imagetruecolortopalette函数有时“效果不如预期的那样好......” – 2011-04-13 18:07:56