how to wrap text in imagemagick

…衆ロ難τιáo~ 提交于 2019-12-08 03:16:54

问题


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 imagemagick";
$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


回答1:


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.




回答2:


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



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/3577870/how-to-wrap-text-in-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!