PHP gdLib 8-Bit PNG with alpha
Asked Answered
P

6

8

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

Parting answered 19/11, 2009 at 9:27 Comment(0)
M
5

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);
?>
Marni answered 24/2, 2010 at 4:9 Comment(0)
S
1

@Sonny

false assumption: PNG of any bit depth can have transparency. It is recorded in the tRNS chunk of the png image (except for truecolor ones) cf format definition

cf www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.tRNS

idem www.w3.org/TR/PNG-Chunks.html#C.tRNS

The difference is how it is recorder: RGBA has a unique record per pixel, with 4 values (3 colors and 1 alpha channel), where "paletted" PNG records alpha channel in its own chunk.

Fireworks is very good at it.

Examples:

http://www.libpng.org/pub/png/pngs-img.html

Setsukosett answered 13/8, 2010 at 16:55 Comment(0)
C
0

I think this could help you.

http://roseindia.net/tutorial/php/phpgd/About-transparent.html

Conservator answered 19/11, 2009 at 9:52 Comment(2)
Not really. Thats actually what i do with imagegif - but if i use imagepng, its an 24-Bit imageParting
AFAIK, PNGs with alpha transparency have to be 24 bit. For 8-bit PNGs you have to choose a color from the palette to be transparent.Chlorohydrin
C
0
<?php
// Create a new true color image
$im = new imagecreatetruecolor(100, 100);

// Convert to palette-based with no dithering and 255 colors
imagetruecolortopalette($im, false, 255);

// Save the image
imagepng($im, './paletteimage.png');
imagedestroy($im);
?>

this should make 8bit png

Conservator answered 19/11, 2009 at 13:16 Comment(1)
If i do this, the transparency goes awayParting
E
0

Building on dfilkovi's solution, have you tried using imagesavealpha() to save the full alpha channel information?

Edin answered 23/2, 2010 at 10:52 Comment(0)
P
0

I had to add the line imagecolortransparent($im, $alphabg); to the following code (taken from previous answer) for this to work:

// Fill with alpha background
$alphabg = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagecolortransparent($im, $alphabg);
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);
?>
Passable answered 6/7, 2012 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.