3

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.stack.imgur.com/w8VBI.png
Chris
  • 833
  • 2
  • 17
  • 37
  • A 32bit PNG's alpha channel only has 8bit resolution anyways. 24bits for RGB and 8bits for alpha. – Marc B Jun 21 '12 at 21:24
  • (Incidentally, if you want the image to display _here_, hit the link "edited 17 seconds ago" (or whatever it says now) and hit "rollback" on my edit.) – sarnold Jun 21 '12 at 21:25
  • this cant work easily because you're telling GD to 'hide' 0,0,0 color but what about the 0,2,0 or 2,1,0 colors that are 'black' but not 0,0,0? pls provide the original image to test. –  Jun 21 '12 at 21:55
  • @vlzvl Im not 100% sure what you're saying here - but here is the original PNG image I used http://www.inkscapegallery.net/files/images/apple.png – Chris Jun 21 '12 at 22:42

2 Answers2

5

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);
Musa
  • 96,336
  • 17
  • 118
  • 137
  • 1
    Thank you Musa. I actually tried that earlier, but I guess I had it in the wrong place. Works like a charm now :) – Chris Jun 21 '12 at 23:13
1

Here is the revised code thanks to Musa for anyone having the same issue

function processPNG($pngImage) {
        $black = imagecolorallocate($pngImage, 0, 0, 0);
        imagecolortransparent($pngImage, $black);
        imagealphablending($pngImage, false);
        imagesavealpha($pngImage, true);
    }    


   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);
    if ($extension == "png") processPNG($fullSize);


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

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

imagedestroy($fullSize);
Chris
  • 833
  • 2
  • 17
  • 37