【问题标题】:How to create curved text with PHP Imagick?如何使用 PHP Imagick 创建弯曲文本?
【发布时间】:2015-10-10 09:26:09
【问题描述】:

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

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

【问题讨论】:

    标签: php imagemagick imagick


    【解决方案1】:

    如果你只是想让它弯曲,有一个demo here,代码如下。

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

    如果你真的想要它沿着一条路径,那么我认为你不能在 Imagick 中。您需要创建一个 SVG 文件并将其转换为 PNG 并将其覆盖在图像上 取自 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>
    

    它还需要一个支持文本路径的 SVG 转换器。 ImageMagick 内置的显然不支持 textpath。

    【讨论】:

      【解决方案2】:

      据我所知,没有直接的方法来创建弯曲的文本,但您可以按照以下步骤进行。

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

      如果您的问题没有解决,请告诉我。

      【讨论】:

        猜你喜欢
        • 2015-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-26
        • 2019-02-08
        • 1970-01-01
        • 2014-09-19
        相关资源
        最近更新 更多