Skip to content
Advertisement

How to convert PNG to 8-bit PNG using PHP GD library

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.

Advertisement

Answer

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

function convertPNGto8bitPNG ()

JavaScript

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

JavaScript

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement