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

标签 php colors gd

一段时间以来,我一直在编写脚本来更改 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);

最佳答案

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

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

关于保留亮度和 Alpha 的 PHP GIF/PNG 真彩色滤镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4257880/

相关文章:

php yii 在数据库中找不到事件记录类 ""的表 ""

java - 从 Color 实例获取表示颜色名称的字符串

php - 在贝塞尔曲线中添加颜色的算法

php - 在 php 中使用透明度调整图像大小

php - 有什么方法可以阻止人们上传带有注入(inject)的 GIF 动图?

javascript - 如果发生错误,在表单提交上显示图像

javascript - 选择选项更改时加载图像

php - 将数组数组转换为单个数组的最佳方法

excel - 更改散点图vba excel中特定点的颜色

javascript - 颜色从蓝色跳到紫色