forefront
1 week ago

Add colour overlay to non transparent part of a PNG

Posted 1 week ago by forefront

Hi

I am trying to accomplish having a colour wheel and it will add an overlay to a PNG image with a 50% opacity so you can still see the outlines of the image behind.

Before: https://imgur.com/v1xRixQ

Current: https://imgur.com/67M9iCg

    private function colorImage($url, $r = null, $g = null, $b = null)
    {

        $im = imagecreatefrompng($url);
        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 | $r << 16 | $g << 8 | $b;
                    imagesetpixel($im, $x, $y, $newColor);
                }
            }
        }

        ob_start();

        imagepng($im);
        imagedestroy($im);
        $image_data = ob_get_contents();

        ob_end_clean();

        $image_data_base64 = "data:image/png;base64," . base64_encode($image_data);

        return $image_data_base64;
    }

Objective: https://imgur.com/RxzaxTP

I have mucked around with intervention but no luck there either.

Please sign in or create an account to participate in this conversation.