change color of image pixel by pixel

浪子不回头ぞ 提交于 2019-12-04 19:30:28

You should be able to work with the palette instead of going through every pixel in that case:

<?PHP
    $myRed = 255;
    $myGreen = 0;
    $myBlue = 0;

    $im = imagecreatefrompng('http://s25.postimg.org/ll0dzblan/image.png');
    imageAlphaBlending($im, true);
    imageSaveAlpha($im, true);

    if (imageistruecolor($im))
    {
        $sx = imagesx($im);
        $sy = imagesy($im);
        for ($x = 0; $x < $sx; $x++)
        {
            for ($y = 0; $y < $sy; $y++)
            {
                $c = imagecolorat($im, $x, $y);
                $a = $c & 0xFF000000;
                $newColor = $a | $myRed << 16 | $myGreen << 8 | $myBlue;
                imagesetpixel($im, $x, $y, $newColor );
            }
        }
    } else {
        $numColors = imagecolorstotal($im);
        $transparent = imagecolortransparent($im);

        for ($i=0; $i < $numColors; $i++)
        {
            if ($i != $transparent)
                imagecolorset($im, $i, $myRed, $myGreen, $myBlue, $myAlpha);

        }
    }

    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!