简体   繁体   English

PHP-GD-如何包装文字

[英]PHP - GD - how wrap a text

I created an image 500X200 with white background and text on it. 我创建了带有白色背景和文本的图像500X200。 How can I wrap "Text" if it will be long in GD. 如果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 supports rotation, vertical and horizontal alignments, configurable line heights and leading, word wrap (obviously), character wrap, and text outlines. GD支持旋转,垂直和水平对齐,可配置的行高和前导,自动换行(显然),字符换行和文本轮廓。 While this frankly belongs in a class (or a namespace) due its size, it's in a function. 虽然坦率地说,它的大小属于一个类(或名称空间),但它属于一个函数。 You're free to trim away unneeded functionality. 您可以随意删除不需要的功能。

Example usage: 用法示例:

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

UPDATE: 更新:

And also refer this question 并参考这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM