HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

Image geometry reset

← screenEmbed   retro →
After calling trimImage it leaves the image in a crop mode where the geometry of the canvas is
different from that of the actual image.

When you call another function that uses the images geometry, it will be applied to the 'wrong'
geometry, that of the uncropped image. e.g. When you are calling distortImage with
Imagick::DISTORTION_ARC to produce an arc of text, this produces the unwanted effect of the
text going off the image.

The way to fix this is to reset the geometry for the image through the Imagick function setImagePage
which is called repage in the Image Magick manual.

Example

function imageGeometryReset()
{
    $draw = new \ImagickDraw();

    $draw->setFont("../fonts/Arial.ttf");
    $draw->setFontSize(48);
    $draw->setStrokeAntialias(true);
    $draw->setTextAntialias(true);
    $draw->setFillColor('#ff0000');

    $textOnly = new \Imagick();
    $textOnly->newImage(600, 300, "rgb(230, 230, 230)");
    $textOnly->setImageFormat('png');
    $textOnly->annotateImage($draw, 30, 40, 0, 'Your Text Here');
    $textOnly->trimImage(0);
    $textOnly->setImagePage($textOnly->getimageWidth(), $textOnly->getimageheight(), 0, 0);

    $distort = array(180);
    $textOnly->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);

    $textOnly->setImageMatte(true);
    $textOnly->distortImage(Imagick::DISTORTION_ARC, $distort, false);

    $textOnly->setformat('png');

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