HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

CLUT image

← clampImage   coalesceImages →
Does something with colours.

I have no idea how this is meant to work. It is not documented particularly well upstream. The entire documentation is 'MagickClutImage() replaces colors in the image from a color lookup table.'.

Having to spend so many hours trying to make a single example work on IM7 the same way it worked on IM6 is really quite frustrating.


clut image example

Example

// Make a shape
$draw = new \ImagickDraw();
$draw->setStrokeOpacity(0);
$draw->setFillColor('black');
$points = [
    ['x' => 40 * 3, 'y' => 10 * 5],
    ['x' => 20 * 3, 'y' => 20 * 5],
    ['x' => 70 * 3, 'y' => 50 * 5],
    ['x' => 80 * 3, 'y' => 15 * 5],
];
$draw->polygon($points);
$imagick = new \Imagick();
$imagick->newPseudoImage(
    300, 300,
    "xc:white"
);
$imagick->drawImage($draw);
$imagick->blurImage(0, 10);
//Make a gradient
$draw = new \ImagickDraw();
$draw->setStrokeOpacity(1);
$draw->setFillColor('white');
$draw->point(0, 0);
$draw->setFillColor('red');
$draw->point(0, 1);
$draw->setFillColor('yellow');
$draw->point(0, 2);
$gradient = new Imagick();
$gradient->newPseudoImage(1, 5, 'xc:none');
$gradient->drawImage($draw);
$gradient->setImageFormat('png');
//These two are needed for the clutImage to work reliably.
$imagick->setImageAlphaChannel(\Imagick::ALPHACHANNEL_DEACTIVATE);
$imagick->transformImageColorspace(\Imagick::COLORSPACE_GRAY);
// $imagick->setImageInterpolateMethod(\Imagick::INTERPOLATE_INTEGER);
//Make the color lookup be smooth
$gradient->setImageInterpolateMethod(\Imagick::INTERPOLATE_BILINEAR);
//Nearest neighbour uses exact color values from clut
//$gradient->setImageInterpolateMethod(\Imagick::INTERPOLATE_NEARESTNEIGHBOR);
$imagick->setImageChannelMask(Imagick::CHANNEL_RGBA);
$imagick->clutImage(
    $gradient,
      \Imagick::INTERPOLATE_NEARESTNEIGHBOR
);
$imagick->setImageFormat('png');
header("Content-Type: image/png");
echo $imagick->getImageBlob();