2012-07-13 53 views
3

我正在訓練我的PHP技能來生成圖像。我只需要一件不起作用的東西。我不認爲在我想要的方式上是可能的,但應該有另一種方式。我已經有代碼是這樣的:PHP imagettftext部分大膽

<?php 
$textSize = 10; 
$text = addslashes("Wietse: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada aliquet dolor, vitae tristique lectus imperdiet nec. Pellentesque et."); 
$maxWidth = 448; 

//Create image 
$im = imagecreate(468, 60); 

// Black background and White text 
$bg = imagecolorallocate($im, 0, 0, 0); 
$textColor = imagecolorallocate($im, 255, 255, 255); 

// Split in multiple lines 
$words = explode(' ', $text); 
$lines = array(); 
$line = ""; 
foreach ($words as $word) { 
    $box = imagettfbbox($textSize, 0, './arial.ttf', $line . $word); 
    $width = $box[4] - $box[0]; 
    if($width > $maxWidth) { 
     $line = trim($line); 
     $line .= "\n"; 
    } 
    $line .= $word . ' '; 
} 

// Write the text in the image 
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arial.ttf', $line); // write text to image 

// Output the image 
header('Content-type: image/png'); 
imagepng($im); 
imagedestroy($im); 
?> 

導致這個形象:

Image

它工作正常,但我的名字,Wietse,應該大膽。我認爲不可能以簡單的方式做到這一點,但它應該是可能的。我可以添加另一個帶有粗體Arial字體的imagettftext(),但是Lorum ipsum文本應該在名稱旁邊開始,第二行應該繼續與名稱相同的X座標。就像現在一樣。 我也有另一個願望,但我認爲這太難了,因爲這個文本可以有任何位置。但是如果有人知道如何做到這一點,那他很棒。這是鏈接(http://www.google.com)有一個下劃線。我不認爲我需要告知更多信息。

謝謝你的目的!

回答

4

我發現了一個解決方案,我自己用粗體表示名稱。我不想解釋所有事情,因爲我是問這個問題的人,所以我只是給出新的代碼和結果圖像。明天我會嘗試給鏈接下劃線。如果有人知道答案,我會接受這個答案。請注意,我已經更改了一些代碼,這不是代碼工作所必需的。

的代碼:

<?php 
$username = 'WietsedeVries'; 
$textSize = 10; 
$text = addslashes("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada aliquet dolor, vitae tristique http://www.google.com pellentesque et."); 
$maxWidth = 448; 

//Create image 
$im = imagecreate(468, 60); 

// Black background and White text 
$bg = imagecolorallocate($im, 0, 0, 0); 
$textColor = imagecolorallocate($im, 255, 255, 255); 

// Write Bold Username in image 
$usernameTotal = '@' . $username . ':'; 
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arialbd.ttf', $usernameTotal); 

// Create white space with the width of the username 
$usernameBox = imagettfbbox($textSize, 0, './arialbd.ttf', $usernameTotal); 
$usernameWidth = $usernameBox[4] - $usernameBox[0]; 
$whiteSpace = ''; 
$whiteSpaceWidth = 0; 
while($whiteSpaceWidth < $usernameWidth) { 
    $whiteSpace .= ' '; 
    $whiteSpaceBox = imagettfbbox($textSize, 0, './arial.ttf', $whiteSpace); 
    $whiteSpaceWidth = $whiteSpaceBox[4] - $whiteSpaceBox[0]; 
} 

// Split in multiple lines 
$words = explode(' ', $text); 
array_unshift($words, $whiteSpace); 
$lines = array(); 
$line = ""; 
foreach ($words as $word) { 
    $box = imagettfbbox($textSize, 0, './arial.ttf', $line . $word); 
    $width = $box[4] - $box[0]; 
    if($width > $maxWidth) { 
     $line = trim($line); 
     $line .= "\n"; 
    } 
    $line .= $word . ' '; 
} 

// Write the text in the image 
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arial.ttf', $whiteSpace . ' ' . $line); 

// Output the image 
header('Content-type: image/png'); 
imagepng($im); 
imagedestroy($im); 
?> 

這導致在該圖像中:

Image

+0

的代碼不給上述輸出 – h2O 2013-10-08 14:33:50

+0

如果任何人遇到這種解決方案;解決方案是使用不同的'.ttf'文件。由於斜體/粗體/穿透/下劃線實際上是完全不同的字體,所以很容易找到所需字體的粗體/斜體版本。看看OP如何使用'imagettfbbox($ textSize,0,'./arialbd.ttf',$ usernameTotal);'爲粗體和'imagettfbbox($ textSize,0,'./arial.ttf',$ line。$ word );'爲文本的其餘部分? 2個不同的'.ttf'文件 – IsThisJavascript 2018-02-09 14:34:28

0

對於最好的可視化,使用-$textColor;這將顯示更好的文本視圖。

2

我知道這是老問題,但如果有人搜索此,這裏是我到達的解決方案..

在幾乎所有的字體,這個解決方案將工作..

如果要創建這個文本:

imagettftext($jpg_image, 35, 0, $x, $y, $color, $font_path, $text2); 

,如果你想大膽,你應該添加這些:

imagettftext($jpg_image, 35, 0, $x, $y, $color, $font_path, $text2); 
imagettftext($jpg_image, 35, 0, $x, $y+1, $color, $font_path, $text2); 
imagettftext($jpg_image, 35, 0, $x, $y+2, $color, $font_path, $text2); 
imagettftext($jpg_image, 35, 0, $x+1, $y, $color, $font_path, $text2); 
imagettftext($jpg_image, 35, 0, $x+2, $y, $color, $font_path, $text2); 

希望它可以幫助你所有^ _^..

對不起,我的英語,我來自格魯吉亞(國家);

1

嘗試使用輪廓/描邊。如何,請檢查此代碼。

function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) { 
    for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++) 
     for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++) 
      $bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text); 
    return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text); 
} 

$font_color = imagecolorallocate($im, 255, 255, 255); 
$stroke_color = imagecolorallocate($im, 0, 0, 0); 
imagettfstroketext($im, 60, 10, 300, 130, $font_color, $stroke_color, "wqy-microhei.ttc", "簡體繁體", 2); 

結果:http://wmh.github.io/hunbook/_static/examples/gd-imagettftext/003.jpg

去:http://wmh.github.io/hunbook/examples/gd-imagettftext.html