HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

FX analyze image

← edgeExtend   eyeColorResolution →
Gradients are key to producing complicated effects in Imagick.

However it is not very easily to visualise exactly what is happening in them, as
your eyes are just not very good at comparing shades of gray.

The code below takes a gradient and produces an easy to visualise graph from the gradient.

Example 1

// Analyzes a one pixel wide image to make it easy to see what the
// gradient is doing
function fxAnalyzeImage(\Imagick $imagick)
{
    $graphWidth = $imagick->getImageWidth();
    $sampleHeight = 20;
    $graphHeight = 128;
    $border = 2;

    $imageIterator = new \ImagickPixelIterator($imagick);

    $reds = [];

    foreach ($imageIterator as $pixels) { /* Loop through pixel rows */
        foreach ($pixels as $pixel) { /* Loop through the pixels in the row (columns) */
            /** @var $pixel \ImagickPixel */
            $color = $pixel->getColor();
            $reds[] = $color['r'];
        }
        $imageIterator->syncIterator(); /* Sync the iterator, this is important to do on each iteration */
    }

    $draw = new \ImagickDraw();

    $strokeColor = new \ImagickPixel('red');
    $fillColor = new \ImagickPixel('none');
    $draw->setStrokeColor($strokeColor);
    $draw->setFillColor($fillColor);
    $draw->setStrokeWidth(1);
    $draw->setFontSize(72);
    $draw->setStrokeAntiAlias(true);

    $x = 0;
    $points = [];
    
    foreach ($reds as $red) {
        $pos = $graphHeight - ($red * $graphHeight / 256);
        $points[] = ['x' => $x, 'y' => $pos];
        $x += 1;
    }

    $draw->polyline($points);

    $plot = new \Imagick();
    $plot->newImage($graphWidth, $graphHeight, 'white');
    $plot->drawImage($draw);

    $outputImage = new \Imagick();
    $outputImage->newImage($graphWidth, $graphHeight + $sampleHeight, 'white');
    $outputImage->compositeimage($plot, \Imagick::COMPOSITE_ATOP, 0, 0);

    $imagick->resizeimage($imagick->getImageWidth(), $sampleHeight, \Imagick::FILTER_LANCZOS, 1);

    $outputImage->compositeimage($imagick, \Imagick::COMPOSITE_ATOP, 0, $graphHeight);
    $outputImage->borderimage('black', $border, $border);

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

Example 2

public function example1()
{
    $graphWidth = 256;
    $imagick = new \Imagick();
    $imagick->newPseudoImage($graphWidth, 1, 'gradient:black-white');
    $arguments = array(9, -90);
    $imagick->functionImage(\Imagick::FUNCTION_SINUSOID, $arguments);
    fxAnalyzeImage($imagick);
}

Example 3

public function example2()
{
    $graphWidth = 256;
    $imagick = new \Imagick();
    $imagick->newPseudoImage($graphWidth, 1, 'gradient:black-white');
    fxAnalyzeImage($imagick);
}

Example 4

public function example3()
{
    $graphWidth = 256;
    $imagick = new \Imagick();
    $imagick->newPseudoImage($graphWidth, 1, 'gradient:black-white');
    $imagick->gammaimage(2);
    fxAnalyzeImage($imagick);
}

Example 5

public function example4()
{
    $graphWidth = 256;
    $imagick = new \Imagick();
    $imagick->newPseudoImage($graphWidth, 1, 'gradient:black-white');
    $arguments = array(9, -90);
    $imagick->functionImage(\Imagick::FUNCTION_SINUSOID, $arguments);
    $fx = "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503";
    $fxImage = $imagick->fxImage($fx);
    fxAnalyzeImage($fxImage);
}