Andrei Oniga
Andrei Oniga

Reputation: 8559

How to create curved text with PHP Imagick?

How can curved text be obtained using Imagick (PHP)? I did not expect not to find a straightforward method or group of methods for this, but it happened...

I did find ImageMagick commands to obtain curved text (http://www.imagemagick.org/Usage/fonts/#arch), but I'm in need for an Imagick (PECL extension) -based script.

Upvotes: 1

Views: 2691

Answers (2)

Danack
Danack

Reputation: 25701

If you just want it curved, there is a demo here with the code below.

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

If you actually want it along a path, then I don't think you can in Imagick. You would need to create an SVG file and convert that to a PNG and overlay it on an image Example of a textpath taken from https://developer.mozilla.org/en/docs/Web/SVG/Element/textPath

<svg width="100%" height="100%" viewBox="0 0 1000 300"
     xmlns="http://www.w3.org/2000/svg" 
     xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <path id="MyPath"
          d="M 100 200 
             C 200 100 300   0 400 100
             C 500 200 600 300 700 200
             C 800 100 900 100 900 100" />
  </defs>

  <use xlink:href="#MyPath" fill="none" stroke="red"  />

  <text font-family="Verdana" font-size="42.5">
    <textPath xlink:href="#MyPath">
      We go up, then we go down, then up again
    </textPath>
  </text>

  <!-- Show outline of the viewport using 'rect' element -->
  <rect x="1" y="1" width="998" height="298"
        fill="none" stroke="black" stroke-width="2" />
</svg>

which also needs an SVG converter that supports textpath. The one built into ImageMagick does not support textpath apparently.

Upvotes: 2

Jitendra Kumar. Balla
Jitendra Kumar. Balla

Reputation: 1213

According to my knowledge there is not direct method for creating curved text, but you can do by following procedure.

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


$text = new Imagick();
$text->newImage(300, 300, "red");
$text->setImageFormat('png');
$text->annotateImage($draw, 30, 40, 0, 'Jitendra Kumar');

$text->trimImage(0);

$text->setImagePage($text->getimageWidth(), $text->getimageheight(), 0, 0);

$distortPoints = array( 120 );
$text->setImageVirtualPixelMethod( Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);


$text->setImageMatte( TRUE );
$text->distortImage(Imagick::DISTORTION_ARC, $distortPoints, FALSE);

$text->setformat('png');

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

Once your problem not solved let me know.

Upvotes: 0

Related Questions