Convert PNG files to 8 Bit PNG wih alpha support

Status
Not open for further replies.

Ifirst

Active Member
281
2011
133
0
Hello guys

Basically i am trying to create a function that will automatically convert 32/24 bit png file to 8bit PNG with alpha support and good quality

However all solutions i tried with php gd give me low quality png files
My function is similar to code below
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);
}
I found something while googling and its called pngquant.org
Sites like TinyPNG use it and i would like to integrate this in my php script.

I am still learning php and i suppose the way to execute command line instructiond for pngquant would be to use the
command
I am coding my script on a WAMP atm and i don't have a linux system at hand.

Can anyone be kind enough to show me a simple code snippet to make pngquant work in php.
Also i would like to know what stuff need to install on a Centos server to support pngquant

Regards
 
5 comments
You are already on the right track. You just need to pass the info through exec command. Example:
PHP:
exec('"pngquant.exe" -ext _opt.png ' . $path);
* Download the files and place it in the same folder as the script you are running
 
wow soft2050 , never though it would be so simple........
You are a life saver
Rep added + liked
One more question
pngquant.exe
should be replaced by which file if server OS is centos 6
Don't see centos binary:(
 
Status
Not open for further replies.
Back
Top