Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PNG 24 bit clean Transparency

I have been trying to get a PNG to upload with a clean, 24 bit alpha transparency. After doing a lot of research, I have managed to get it sort of working, however the transparency seems to be low quality 8 bit as you can see here in this screenshot:

http://cozomo.com/apple.png

Any help to achieve a clean PNG upload and resize with 24 bit smooth transparency would be much appreciated. My current code is below.

if($extension=="png")
    {
        $uploadedfile = $_FILES['photo']['tmp_name'];
        $src = imagecreatefrompng($uploadedfile);
    }

        $dest_x = 1400; 
        $dest_y = 1200;

        if ($width > $dest_x or $height > $dest_y) { 

                if ($width >= $height) { 
                    $fullSize_x = $dest_x; 
                    $fullSize_y = $height*($fullSize_x/$width); 
                } else { 
                    $fullSize_x = $width*($fullSize_y/$height); 
                    $fullSize_y = $dest_y; 
                } 
        }

        $fullSize=imagecreatetruecolor($fullSize_x,$fullSize_y);

    //TEST
    $black = imagecolorallocate($fullSize, 0, 0, 0);
    imagecolortransparent($fullSize, $black);
    //TEST END

    // OUTPUT NEW IMAGES
    imagecopyresampled($fullSize,$src,0,0,0,0,$fullSize_x,$fullSize_y,$width,$height);

    imagepng($fullSize, "/user/photos/".$filename);

    imagedestroy($fullSize);


  [1]: https://i.sstatic.net/w8VBI.png
like image 419
Chris Avatar asked Nov 26 '25 06:11

Chris


1 Answers

To save the full alpha channel you'll have to use imagesavealpha, put this before you save the png

imagealphablending($fullSize, false);
imagesavealpha($fullSize, true);
like image 55
Musa Avatar answered Nov 28 '25 20:11

Musa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!