开发者

how to wrap text in imagemagick

I was able to figure a basic word wrap function like this

 $draw = new ImagickDraw();
 $x = 0;
 $y=20;
 $angle = 0;
 $str = "some text for testing of a word wrap in imagemagic开发者_运维百科k";
$str = wordwrap($str, 10,"\r");
$im->annotateImage( $draw, $x, $y, $angle, $str );

and that seems to work ok except that the tracking i think its called you know the space between lines is too much and thoughts or ideas on how to fix this or if there is a better option


The line height is determined by the font metric. You could of course add a blank line otherwise you would need to render one line at a time and manually specify the offset of the text within the image.

[EDIT] : On OP request, there appears to be a command-line version of it.


Some refactoring:

$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;
}


Sine I could control the spacing I went with rendering the lines each

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


You can have ImageMagic calculate the metrics details for you: http://php.net/manual/en/function.imagick-queryfontmetrics.php.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜