【问题标题】:PHP GIF/PNG True Colorize Filter that Preserves Luminosity and AlphaPHP GIF/PNG True Colorize 过滤器,保留亮度和 Alpha
【发布时间】: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

标签: php colors gd


【解决方案1】:

听起来你想要一个有色灰度。这支持透明度...

http://www.exorithm.com/algorithm/view/duotone_image

【讨论】:

  • 这看起来很有趣。我上传了一张图片到他们的测试中,但它没有用。让我担心它是否真的有效,但我会看看我会得到什么影响。
  • 对,所以我试了一下,这与 PHP Filter 'Colorize' 的作用相同。它没有保留图像中的黑色。然而,它确实保留了白色。
  • 好的,在exorithm.com/algorithm/view/duotone_image 再试一次,但这次设置 pcnt 标志
  • 这已经足够接近了。它有点偏爱白色,但我可以补偿。
【解决方案2】:

$im = imagecreatefrompng($filename);之后尝试这些功能

imagealphablending($im, false);
imagesavealpha($im, true);

并使用 imagecreatetruecolor() :

$nim = imagecreatetruecolor ( imagesx($im), imagesy($im) );

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 2011-12-31
    • 1970-01-01
    • 2011-01-09
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    相关资源
    最近更新 更多