PHP GIF/PNG 真实彩色滤镜,保留亮度和 Alpha

发布于 2024-10-03 15:52:00 字数 1547 浏览 1 评论 0原文

我一直在编写一个脚本来更改 GIF 和 PNG 文件的颜色,该脚本比 PHP 着色过滤器效果更好,后者不会保留亮度。我已经想出了这个,但效果不太好:

$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);

I've been working on a script for a while now to change the colors of GIF and PNG files that works better than the PHP colorize filter, which doesn't preserve luminosity. I've come up with this, but it doesn't quite work right:

$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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还如梦归 2024-10-10 15:52:00

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

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

Sounds like you want a tinted grayscale. This supports transparency...

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

层林尽染 2024-10-10 15:52:00

$im = imagecreatefrompng($filename); 之后尝试使用这些函数

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

并使用 imagecreatetruecolor() :

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

Try with these functions after $im = imagecreatefrompng($filename);

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

And use imagecreatetruecolor() :

$nim = imagecreatetruecolor ( imagesx($im), imagesy($im) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文