HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

Levelize image

← listColors   layerPSD →

The ImageMagick library has a Reversed Level Adjustment function. This function is not exposed in through the ImageMagick Wand API and so it is not usable in Imagick. However we can achive the same result by using the evaluate command in two steps.

So to do the equivalent of:

convert -size 300x300 gradient:black-white +level 50x100%

Or more generally:

convert -size 300x300 gradient:black-white +level ${blackPoint}x${whitePoint}%

You can use use Imagick::evaluateImage with a separate EVALUATE_POW, EVALUATE_MULTIPLY, EVALUATE_ADD


Example

function levelizeImage($blackPoint, $gamma, $whitePoint)
{
    $imagick = new \Imagick();
    $imagick->newPseudoimage(300, 300, 'gradient:black-white');
    $maxQuantum = $imagick->getQuantum();
    $imagick->evaluateimage(\Imagick::EVALUATE_POW, 1 / $gamma);
    
    //Adjust the scale from black to white to the new 'distance' between black and white
    $imagick->evaluateimage(\Imagick::EVALUATE_MULTIPLY, ($whitePoint - $blackPoint) / 100);

    //Add move the black point to it's new value
    $imagick->evaluateimage(\Imagick::EVALUATE_ADD, ($blackPoint / 100) * $maxQuantum);
    $imagick->setFormat("png");

    header("Content-Type: image/png");
    echo $imagick->getImageBlob();
}