HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelTutorial Source codeReport an issue


← Quantum   queryFontMetrics →

TreeDepth - Normally, this integer value is zero or one. A zero tells Quantize to choose a optimal tree depth of Log4(number_colors). A tree of this depth generally allows the best representation of the reference image with the least amount of memory and the fastest computational speed. In some cases, such as an image with low color dispersion (a few number of colors), a value other than Log4(number_colors) is required. To expand the color tree completely, use a value of 8.

// Example 1
function quantizeImage($image_path, $numberColors, $colorSpace, $treeDepth, $dither)
    $imagick = new \Imagick(realpath($image_path));
    $imagick->quantizeImage($numberColors, $colorSpace, $treeDepth, $dither, false);
    header("Content-Type: image/png");
    echo $imagick->getImageBlob();
// Example 2 - s
function quantizeImages($numberColors, $colorSpace, $treeDepth, $dither)
    set_time_limit(120);        //This takes a long time
    ini_set('memory_limit', '128M'); //And uses a lot of memory
    $image_pathPattern = "../public/images/spiderGif";
    $fileIterator = new \GlobIterator(realpath($image_pathPattern).'/*.png');

    $imagick = new \Imagick();
    $count = 0;
    foreach ($fileIterator as $fileEntry) {
        if ((($count++) % 3) != 0) {
        $nextImage = new \Imagick(realpath($fileEntry));

    $imagick->quantizeImages($numberColors, $colorSpace, $treeDepth, $dither, false);
    $imagick->setImageIterations(0); //loop forever

    header("Content-Type: image/gif");
    echo $imagick->getImagesBlob();