代码之家  ›  专栏  ›  技术社区  ›  mcgrailm

如何在ImageMagick中包装文本

  •  1
  • mcgrailm  · 技术社区  · 15 年前

    我能想到一个基本的自动换行功能

     $draw = new ImagickDraw();
     $x = 0;
     $y=20;
     $angle = 0;
     $str = "some text for testing of a word wrap in imagemagick";
    $str = wordwrap($str, 10,"\r");
    $im->annotateImage( $draw, $x, $y, $angle, $str );
    

    这似乎行得通,除了追踪,我认为它叫做,你知道行与行之间的空间太大了,关于如何解决这个问题,或者如果有更好的选择

    4 回复  |  直到 10 年前
        1
  •  2
  •   shamittomar    15 年前

    行高由字体度量确定。当然,您可以添加一个空行,否则您将需要一次呈现一行,并手动指定图像中文本的偏移量。

    [编辑] :在OP请求时,似乎有一个 command-line version of it .

        2
  •  0
  •   mcgrailm    15 年前

    我可以控制绘制每一行所用的间距。

      $draw = new ImagickDraw();
      $x = 0;
      $y=20;
      $angle = 0;
      $padding = 10;
      $str = "some text for testing of a word wrap in imagemagick";
      $str = wordwrap($str, 10,"\r");
      $str_array = explode("\n",$str);
      foreach($str_array as $line)
        $im->annotateImage( $draw, $x, $y+$padding, $angle, $line );
      }
    
        3
  •  0
  •   Marc B    15 年前

    您可以让ImageMagic为您计算度量详细信息: http://php.net/manual/en/function.imagick-queryfontmetrics.php .

        4
  •  0
  •   Mathias    10 年前

    一些重构:

    $string = 'Some random Text here';
    
    $y = 120;
    $line_height = 50;
    $str = wordwrap($string, 20,"\n");
    $str_array = explode("\n",$str);
    foreach($str_array as $line){
        $image->annotateImage($draw, 0, $y, 0, $line );
        $y += $line_height;
    }