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.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
To convert any PNG image to 8-bit PNG use this function, I've just created
function convertPNGto8bitPNG ()
Parameters
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
Example (original -> 8-bit)
(Source: pfc.png) ORIGINAL PNG IMAGE
(Destination: pfc8bit.png) CONVERTED PNG IMAGE (8-bit)
Hope someone finds this helpful.
Instead of GD library I strongly recommend using pngquant 1.5+ commandline using
exec()
orpopen()
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):pngquant supports alpha transparency very well.