View on GitHub

GDIndexedColorConverter

PHP library - convert an image to indexed color mode.

Download this project as a .zip file Download this project as a tar.gz file

GDIndexedColorConverter

GDIndexedColorConverter is a simple library that convert an image into indexed color mode. With indexed color mode, an image can be displayed with only a few specific colors.

To archieve image dithering effect, GDIndexedColorConverter uses Floyd–Steinberg dithering algorithm to apply error diffusion of each pixel onto its neighboring pixels.

Requirements

Since GDIndexedColorConverter uses some functions of the GD extension, you need to the enable GD extension in the PHP configuration file (php.ini).

Usage

GDIndexedColorConverter provide a function named convertToIndexedColor to convert an image into indexed color mode, it accepts three parameters(listed below), and return a new image resource of indexed color mode.

Code example:

    // create an image
    $image = imagecreatefromjpeg('example.jpg');

    // create a gd indexed color converter
    $converter = new GDIndexedColorConverter();

    // the color palette
    $palette = array(
        array(0, 0, 0),
        array(255, 255, 255),
        array(255, 0, 0),
        array(0, 255, 0),
        array(0, 0, 255)
    );

    // convert the image to indexed color mode
    $new_image = $converter->convertToIndexedColor($image, $palette, $dither_amount);

    // save the new image
    imagepng($new_image, 'example_indexed_color.png', 0);

License

GDIndexedColorConverter is licensed under the MIT license.

Copyright (c) 2014