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.
How to convert PNG to 8-bit PNG using PHP GD library
10.2k views Asked by Nilesh At
2
There are 2 answers
0
Kornel
On
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):

pngquant supports alpha transparency very well.
Related Questions in PHP
- How to add the dynamic new rows from my registration form in my database?
- Issue in payment form gateway
- How to create a facet for WP gridbuilder that displays both parent and child custom fields?
- Function in anonymous Laravel Blade component
- How to change woocomerce or full wordpress currency with value from USD to AUD
- General questions about creating a custom theme Moodle CMS
- How to add logging to an abstract class in php
- error 500 on IIS FastCGI but no clue despite multiple error loggings activated
- Composer installation fails and reverts ./composer.json and ./composer.lock to original content
- How to isolate PHP apps from each other on a local machine(Windows or Linux)?
- Laravel: Using belongsToMany relationship with MongoDB
- window.location.href redirects but is causing problems on the webpage
- Key provided is shorter than 256 bits, only 64 bits provided
- Laravel's whereBetween method not working with two timestamps
- Implementing UUID as primary key in Laravel intermediate table
Related Questions in PNG
- How change path to my new generated webp image in index.html?
- Python canvas save drawing problem with postscript sizes
- Serialize 4 png Images into a Flatbuffer Array
- How can I write the first bytes of a .png image in Java when only signed bytes are supported?
- Oscilloscope graph data cannot be converted to graphs
- Hide elements of htmlwidget plot before saving as PNG
- Is there a way to convert emf/wmf files to png/jpg in python?
- Could converting a JPG to PNG or PPM be a good idea?
- What is the difference between converting PNG images to JPEG using convertio.co
- Blender image sequence (png) to Unity
- How to plot multiple VennDiagrams in png file from within a function in R
- Visio VBA export to PNG in high resolution?
- How we can download an image with PNG format in Power Apps?
- cypress mochawesome reporter screenshot as base64
- How can I display a Base64 encoded image in Android using Coil?
Related Questions in GDLIB
- gd library watermark in png image
- Image transform in PHP
- Undefined function imagecreatefromjpeg in php8 with gd installed
- PHP GD library - All colors added are Transparent
- Problems installing GD for Circos on Mac OS
- How to write Hindi text property in Image with php
- How to make a color transparent in gd lib after merging images?
- GD LIB how to convert every uploaded images successfully into jpg type?
- Normalize CSS filters with GD lib filters
- merge multiple images and text into single image but not able to arrange properly
- Placing a Transparent PNG onto another Transparent PNG (Bottom Image not showing)
- Remove image background is not working properly in php imagick
- PHP Image to Color-able image using GDLib
- GD Library imagettftext getting problem in Gujarati Language Text
- Text is not showing on image create by gd library
Related Questions in PNG-8
- Use png8 instead of png24 as RGB height map
- Save PNG as PNG-8 with Floyd-Steinberg 2 dithering like GIMP in PHP
- How can you tell whether the file you've downloaded in PNG-8, PNG-24, or PNG-32?
- What is difference between png8 and png24
- Handling weird png-8 with ImageIO
- writing 8-bit png with a transparent color from C++ in Windows
- PHP GD only: PNG24+Alpha => PNG8 does not save Alpha
- Imagemagick lower PNG depth
- Create Paletted CGImageRef
- How to remove the alpha channel of a png-24 image by java?
- Convert 32 bit png to 8 bit png with ImageMagick by preserving semi transparent pixels
- Reduce Color Count in PNG-8 Palette using FreeImage Library (C#)
- How to save 8-bit PNGs with ChunkyPNG or RMagick
- How to convert PNG to 8-bit PNG using PHP GD library
- Create and write paletted RGBA PNG using NSImage
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
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
$sourcePathexists and$destPathis 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.