How to convert PNG to 8-bit PNG using PHP GD library
Asked Answered
E

2

7

I want to write a routine which takes PNG image path as parameter and convert that image into 8-bit PNG image. I need to use PHP GD library for this.

Erose answered 22/4, 2011 at 5:25 Comment(3)
What is your question? It's nice that you're telling us your TODO list for the day, but that doesn't make it a question.Spadiceous
ImageMagick would be a better option.Harker
@Znarkus I wasn't sure whether it was possible in GD at all, but @Wh1T3h4Ck5 proved me wrong.Harker
M
14

To convert any PNG image to 8-bit PNG use this function, I've just created

function convertPNGto8bitPNG ()

 function convertPNGto8bitPNG ($sourcePath, $destPath) {

     $srcimage = imagecreatefrompng($sourcePath);
     list($width, $height) = getimagesize($sourcePath);

     $img = imagecreatetruecolor($width, $height);
     $bga = imagecolorallocatealpha($img, 0, 0, 0, 127);
     imagecolortransparent($img, $bga);
     imagefill($img, 0, 0, $bga);
     imagecopy($img, $srcimage, 0, 0, 0, 0, $width, $height);
     imagetruecolortopalette($img, false, 255);
     imagesavealpha($img, true);

     imagepng($img, $destPath);
     imagedestroy($img);

 }

Parameters

  • $sourcePath - Path to source PNG file
  • $destPath - Path to destination PNG file

Note

I recommend to make sure that $sourcePath exists and $destPath is writable before running this code. Maybe this function won't work with some transparent images.

Usage

convertPNGto8bitPNG ('pfc.png', 'pfc8bit.png');

Example (original -> 8-bit)

(Source: pfc.png) ORIGINAL PNG IMAGE

enter image description here

(Destination: pfc8bit.png) CONVERTED PNG IMAGE (8-bit)

enter image description here

Hope someone finds this helpful.

Miseno answered 22/4, 2011 at 8:17 Comment(12)
@Nilesh, I've just tried with few transparent PNG's and it works? Send me a link to transparent PNG you use.Miseno
Image that I have used is: google.co.in/imgres?imgurl=http://www.axdn.com/redist/…Erose
@Nilesh, YES, you were right... I forgot to set transparent color. Function above now has new body... try it again.Miseno
@Wh1T3h4Ck5, Yes, it is working now. But image is not clear yet. I have tried with some other images as wellErose
@Nilesh, I was researching about this and found that 8-bit PNG is same as GIF because both of them use bit-transparency (don't have alpha channel). Actually, I've found solution to convert any PNG to GIF (transparency, 256 colors) but when I try to convert it to transparent PNG, it turns into 32-bit (I guess, because most of GD functions return 32-bit results). But later I've found that, even 'properties' window says that image I've created this way is 32-bit PNG, Photoshop recognizes them as PNG-8 and those images have actual size of 8-bit PNGs!? Strange behavior...Miseno
Unlike GIF, you are able to use alpha transparency with 8-bit PNGs, though there are not many programs that are able to produce them. See blogs.sitepoint.com/png8-the-clear-winnerSingletary
@Marcel - According to that link It offers GIF-like 1-bit transparency. Pixels are either solid or completely transparent, but never partially see-through. You can't use alpha transparency against PNG-8 images because it doesn't have alpha level (like e.g. PNG 32-bit). In other words, PNG-8 uses solid color as transparent (exactly what GIF does)Miseno
If read one sentence below that point, you read “Although this last point is generally accepted as fact, it isn’t strictly true, and this is the topic we’ll be examining today.” ;-)Singletary
@Marcel, also look my answer here. You can get full alpha transparency by using PNG 32-bit only (RGBA - Red-Green-Blue-Alpha).Miseno
@Marcel, however, thank you for this link, I'll read this complete article and see what can I do about this problem.Miseno
So does it mean, it is not possible to create 8-bit transparent png using GD? Thank you guys for all your efforts and your valuable information.Erose
@Nilesh, It's possible. Now I'm looking for solution to accomplish that. We're talking about bit-transparency vs alpha-transparency and how to get 4-bit transparency inside indexed-palette PNG-8. You could see that transparency works on PNG-8, but that's not expected 'good looking' result. I'm busy at the moment, but I'll try to solve this problem as soon as possible.Miseno
D
10

Instead of GD library I strongly recommend using pngquant 1.5+ commandline using exec() or popen() functions.

GD library has very poor-quality palette generation code.

Same image as in the other answer, same file size as GD library, but converted using pngquant to merely 100 colors (not even 256):

enter image description here

pngquant supports alpha transparency very well.

Dickie answered 17/12, 2011 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.