PHP Imagick在弧剪裁中扭曲文本

时间:2014-03-20 17:04:49

标签: fonts imagemagick imagick

我尝试使用Imagick在PHP中扭曲一些文本。这是我的代码......

$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 );  

问题是......文本被剪裁到图像外部。我做错了什么?

输出图片: http://picload.org/thumbnail/laiiplw/text.png

1 个答案:

答案 0 :(得分:1)

显然trimImage将图像保留为裁剪模式,其中画布的几何图形与实际图像的几何图形不同。

当应用弧线时,它会使用“错误”。几何图形,即未剪切图像的几何图形,它会产生文本偏离图像的不良影响。

解决这个问题的方法是通过Imagick函数setImagePage重置图像的几何图形,这在Image Magick手册中称为repage,即

<?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();

生成图像:

enter image description here