PHP - GD - 如何包装文本


PHP - GD - how wrap a text

我创建了一个500X200的图像,上面有白色背景和文字。如果"文本"在GD中很长,我该如何包装。

$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255); 
$black = imagecolorallocate($image, 0, 0, 0);
imagefill ( $image, 0, 0, $white );
...
imagettftext($image, 18, 0, $x, 100, $black, $font, "text");
GD

支持旋转,垂直和水平对齐,可配置的行高和行距,自动换行(显然),字符换行和文本轮廓。虽然坦率地说,由于其大小,它属于类(或命名空间),但它位于函数中。您可以自由地修剪不需要的功能。

用法示例:

$size = 14;
$angle = 34;
$left = 10;
$top = 40;
$color = imagecolorallocate($im, 255, 0, 0);
$fontfile = "arial.ttf";
$text = "Test";
$opt = array('width' => 300,'line_height' => 15,'orientation' =>array(ORIENTATION_TOP, ORIENTATION_LEFT),
'align' => ALIGN_LEFT,
'v_align' => VALIGN_TOP,
'outlines' = array(
array(5, imagecolorallocate($im, 0, 255, 0)),
array(2, imagecolorallocate($im, 0, 0, 255)),
),
// More options 
);
imagettftextboxopt($im, $size, $angle, $left, $top, $color, $fontfile, $text, $opt);

更新:

并参考这个问题