2012-12-30 39 views
3

當我使用此代碼,我可以做一個形象出來的文字,但在單行線,如何在多行(段落)中寫文本?

function writetext($image_path,$imgdestpath,$x,$y,$angle,$text,$font,$fontsize) { 
     $image=imagecreatefromjpeg("$image_path"); 
     $height = imageSY($image); 
     $width = imageSX($image); 
     $color = imagecolorallocate($image,0,0,0); 
     $textwidth = $width; 
     imageTTFtext($image,$fontsize,$angle,$x,$y,$color,$font, $text); 
     ImageJPEG($image,$imgdestpath); 
} 

請告知如何使這個圖像中的多段?

回答

6

對於每一個你需要新的imageTTFtext功能線與新價值堪稱爲$ Y 例如:

$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer non nunc lectus.  Curabitur hendrerit bibendum enim dignissim tempus. Suspendisse non ipsum auctor metus consectetur eleifend. Fusce cursus ullamcorper sem nec ultricies. Aliquam erat volutpat. Vivamus massa justo, pharetra et sodales quis, rhoncus in ligula. Integer dolor velit, ultrices in iaculis nec, viverra ut nunc.'; 

// Break it up into pieces 125 characters long 
$lines = explode('|', wordwrap($text, 115, '|')); 

// Starting Y position 
$y = 513; 

// Loop through the lines and place them on the image 
foreach ($lines as $line) 
{ 
imagettftext($image, $font_size, 0, 50, $y, $font_color, $font, $line); 

// Increment Y so the next line is below the previous line 
$y += 23; 
} 

source

+0

的情況下什麼,如果我把一個段落的輸入(有中斷)即$ text =「你好,你好嗎?」
你需要什麼?
hello world「; 如何在圖片上寫下相同的內容? – pushE