phppnggdpng-8

PHP gdLib 8-Bit PNG with alpha


how is it possible to save my image, created with gd, as an png-8?

it saves as gif with transparent channel well - but I want to use png-8.

Best Regards, Beerweasle


Solution

  • Using imagesavealpha() and a transparent bg color should do the trick...

    Based on dfilkovi's code:

    <?php
    // Create a new true color image
    $im = new imagecreatetruecolor(100, 100);
    
    // Fill with alpha background
    $alphabg = imagecolorallocatealpha($im, 0, 0, 0, 127);
    imagefill($im, 0, 0, $alphabg);
    
    // Convert to palette-based with no dithering and 255 colors with alpha
    imagetruecolortopalette($im, false, 255);
    imagesavealpha($im, true);
    
    // Save the image
    imagepng($im, './paletteimage.png');
    imagedestroy($im);
    ?>