2010-08-26 12 views
1

我能弄清楚这样如何包装文字的ImageMagick

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

一个基本的自动换行功能,这似乎只是跟踪我认为它叫你知道线之间的空间,以好的工作太关于如何解决这个问题的想法或想法,或者如果有更好的选择

回答

0

正弦我能控制的间距我去渲染线各

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

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

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

+0

所以我想有没有办法来指定字体指标? – mcgrailm 2010-08-26 17:57:59

+0

@mcgrailm,更新了答案。 – shamittomar 2010-08-26 18:02:32

0

您可以ImageMagic计算指标的详细信息给你:http://php.net/manual/en/function.imagick-queryfontmetrics.php

+0

你能解释一下,或者举个例子说明这是如何让我改变线条之间的空间? – mcgrailm 2010-08-26 20:27:02

+1

您可以从指标数据中获取行高。然后绘制每个单独的文本行,并根据高度指标更改每行的起点并进行调整。 – 2010-08-27 16:57:22

+0

我做了什么MarcB建议 - 得到字体公制线高度,然后添加每行与“annotateImage”设置Y稍微更多htan字体指标规定的行高。所以当我运行指标时,我得到20(字体是16像素)。每行,我写一个annotateImage并将Y设置为$ y + = 26,并且它将在行之间添加6个像素,使其看起来是双倍行距。 – 2012-03-30 07:24:00

0

一些重构:

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