2012-07-13 98 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