I try to distort some text in arc in PHP with Imagick. Here is my code...

$draw = new ImagickDraw();
$draw->setFont('cambria.ttf');
$draw->setFontSize(20);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$draw->setFillColor('#ff0000');
$textOnly = new Imagick();
$textOnly->newImage(500,100, "transparent");
$textOnly->setImageFormat('png');
$textOnly->annotateImage($draw, 11, 25, 0, 'Your Text Here');
$textOnly->trimImage(0);

$distort = array( 180 );
$textOnly->setImageVirtualPixelMethod( Imagick::VIRTUALPIXELMETHOD_TRANSPARENT );
$textOnly->setImageMatte( TRUE );
$textOnly->distortImage( Imagick::DISTORTION_ARC, $distort, FALSE );  

The problem is... the text is clipped to outside the image. What am I doing wrong?

Output image: http://picload.org/thumbnail/laiiplw/text.png

有帮助吗?

解决方案

Apparently trimImage leaves the image in a crop mode where the geometry of the canvas is different from that of the actual image.

When the arc is applied it uses the 'wrong' geometry, i.e. that of the uncropped image, which 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 i.e.

<?php

$draw = new ImagickDraw();
$draw->setFont('Arial.ttf');
$draw->setFontSize(20);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$draw->setFillColor('#ff0000');


$textOnly = new Imagick();
$textOnly->newImage(500, 100, "blue");
$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();

produces an image:

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top