HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

Difference marking

← composite   edgeExtend →
A very simple example to show how to find the differences between two images, mark them with a red outline, and then render the image as an animated gif so that the differences can be seen easily.

Example

function diffMarking()
{
    $image1 = new Imagick(__DIR__."/../../../public/images/compare1.png");
    $image2 = new Imagick(__DIR__."/../../../public/images/compare2.png");

    $image1->compositeImage($image2, \Imagick::COMPOSITE_DIFFERENCE, 0, 0);

    $overlay = clone $image1;
    $overlay->negateImage(false);
    $overlay->setImageAlphaChannel(\Imagick::ALPHACHANNEL_DEACTIVATE);
    $overlay->transformImageColorSpace(\Imagick::COLORSPACE_GRAY);

    $overlay->statisticImage(\Imagick::STATISTIC_MINIMUM, 20, 2);
    $overlay->statisticImage(\Imagick::STATISTIC_MINIMUM, 2, 20);
    $overlay->statisticImage(\Imagick::STATISTIC_GRADIENT, 4, 4);

    $red = new Imagick();
    $red->newPseudoImage(
        $overlay->getImageWidth(),
        $overlay->getImageHeight(),
        'xc:red'
    );

    $red->compositeImage($overlay, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);

    $withOutline = clone $image2;
    $withOutline->compositeImage($red, \Imagick::COMPOSITE_ATOP, 0, 0);

    $outputGif = new Imagick();
    $outputGif->addImage($image2);
    $outputGif->addImage($withOutline);

    $outputGif = $outputGif->deconstructImages();
    $outputGif->setImageFormat('gif');
    header("Content-Type: image/gif");
    echo $outputGif->getImagesBlob();
}