2016-02-29 48 views
1

我创建了一个图像500X200,上面有白色背景和文字。 如何在GD中包含“Text”。PHP - 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"); 
+0

你也许可以使用'imagettfbbox()'函数来确定文本有多大会并将结果与​​图像的宽度进行比较。 'imagettfbbox()'返回一个点数组,因此您需要根据边界框的左侧和右侧进行一些计算。字符串'$ text'然后可以拆分并插入换行符 – RamRaider

回答

2

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

UPDATE:

而且还指该question