2015-10-10 09:26
浏览 101

如何使用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.

图片转代码服务由CSDN问答提供 功能建议

如何使用Imagick(PHP)获取弯曲文本? 我没想到找不到简单的方法或者 这方面的一组方法,但它发生了...

我找到了ImageMagick命令来获取弯曲文本( http://www.imagemagick.org/Usage/fonts/#arch ),但我需要一个基于Imagick(PECL扩展)的脚本。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dousunle5801 2015-10-10 21:06

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

    $draw = new \ImagickDraw();
    $textOnly = new \Imagick();
    $textOnly->newImage(600, 300, "rgb(230, 230, 230)");
    $textOnly->annotateImage($draw, 30, 40, 0, 'Your Text Here');
    $textOnly->setImagePage($textOnly->getimageWidth(), $textOnly->getimageheight(), 0, 0);
    $distort = array(180);
    $textOnly->distortImage(Imagick::DISTORTION_ARC, $distort, false);
    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"
        <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" />
      <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
      <!-- Show outline of the viewport using 'rect' element -->
      <rect x="1" y="1" width="998" height="298"
            fill="none" stroke="black" stroke-width="2" />

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

    打赏 评论
  • dongyu5104 2015-10-10 09:38

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

    $draw = new ImagickDraw();
    $text = new Imagick();
    $text->newImage(300, 300, "red");
    $text->annotateImage($draw, 30, 40, 0, 'Jitendra Kumar');
    $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);
    header("Content-Type: image/png");
    echo $text->getimageblob();

    Once your problem not solved let me know.

    打赏 评论

相关推荐 更多相似问题