dssqq64884 2015-10-10 09:26
浏览 217
已采纳

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

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条