【发布时间】:2011-05-14 13:26:36
【问题描述】:
我一直在编写一个脚本来更改 GIF 和 PNG 文件的颜色,它比 PHP colorize 过滤器效果更好,它不能保持亮度。我想出了这个,但它并不完全正确:
$filename = "images/sprites/".$_GET['sprite'].".png";
$im = imagecreatefrompng($filename);
$nim = imagecreate( imagesx($im), imagesy($im) );
$background = imagecolorallocate($nim, 255, 0, 255);
$size = getimagesize($filename);
for($y = 0; $y < imagesy($nim); $y++) {
for($x = 0; $x < imagesx($nim); $x++) {
$rgb = imagecolorat($im, $x, $y);
$colors = imagecolorsforindex($im, $rgb);
$mods = explode("x",$_GET['color']);
$colors['red'] = ($colors['red'] / 8 + (255 - ((255 - $mods[0] - $colors['red']) * 2))) / 2;
$colors['green'] = ($colors['red'] / 8 + (255 - ((255 - $mods[1] - $colors['green']) * 2))) / 2;
$colors['blue'] = ($colors['red'] / 8 + (255 - ((255 - $mods[2] - $colors['blue']) * 2))) / 2;
$r = $colors['red'];
$g = $colors['green'];
$b = $colors['blue'];
if($r < 0) $r = 0;
if($g < 0) $g = 0;
if($b < 0) $b = 0;
if($r > 255) $r = 255;
if($g > 255) $g = 255;
if($b > 255) $b = 255;
if(!isset($color[$r.$g.$b])) {
$color[$r.$g.$b] = imagecolorallocate($nim, $r, $g, $b);
}
imagesetpixel($nim, $x, $y, $color[$r.$g.$b]);
}
}
imagecolortransparent($nim, 1);
header('Content-Type: image/png');
imagepng($nim);
【问题讨论】:
-
什么是 PHP colorize 过滤器,你想让它做什么?不要忘记:在您当前的代码中什么不起作用?
-
当前代码可以工作,但不太正确。它并没有真正保留亮度,也没有保留 Alpha 通道。 PHP Filter: Colorize 会改变颜色,但不会保留亮度。这是它的页面:php.net/manual/en/function.imagefilter.php