Search code examples
phpimagemagickimagick

PHP Imagick - "-quantize transparent" equivalent


Is there a PHP Imagick equivalent for -quantize transparent ???

-quantize transparent usage example note: seach for '-quantize transparent' within page


Solution

  • Quantize is supported by PHP's Imagick extension; however, little documentation has been authored. Luckily, the example from "Color Quantization and Transparency" is straightforward.

    convert alpha_gradient.png -quantize transparent \
        +dither  -colors 15   alpha_colors_15qt.png
    

    From this example, we can determine the 5 arguments needed by Imagick::quantizeImage().

    • Number of colors = 15 (-colors 15)
    • Colorspace = transparent
    • Tree depth = 0 (undefined)
    • Dither = False (+dither)
    • Messure errors = False
    <?php
    
    $wand = new Imagick("alpha_gradient.png");
    $wand->quantizeImage(15,Imagick::COLORSPACE_TRANSPARENT,0,false,false);
    $wand->writeImage("alpha_colors_15qt.png");