使用imagettftext下划线文本

时间:2013-02-07 00:39:02

标签: php freetype imagettftext

我的问题是,如何为图片中的所有文字加下划线?

代码:

function createImage($text)
{
    $text .= "\n";
    $text = wordwrap($text, 40, "\n");
    $newlines = substr_count($text, "\n");
    if($newlines == 0)
    {
        $height = 30;
    }
    else
    {
        $height = 30*$newlines-$newlines*7;
    }
    putenv('GDFONTPATH=' . realpath('.'));
    header('Content-Type: image/png');

    $im = imagecreatetruecolor(315, $height);
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    $purple = imagecolorallocate($im, 97, 26, 139);
    imagefilledrectangle($im, 0, 0, 399, $height, $white);
    $font = 'arialbd.ttf';

    imagettftext($im, 11, 0, 10, 20, $purple, $font, $text);
    imagepng($im);
    imagedestroy($im);
}

createImage("Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow ");

RESULT http://i.imgur.com/Jdr7HPy.png

我希望所有文字都加下划线,我找到了一些解决方案,但它们只是从左到右,不依赖于单词。

2 个答案:

答案 0 :(得分:4)

使用Unicode下划线组合字符U + 0332。

你需要一个循环或聪明的数组合并来修改文本,

$e = explode(' ', $text);

for($i=0;$i<count($e);$i++) {
  $e[$i] = implode('&#x0332;', str_split($e[$i]));
}

$text = implode(' ', $e);

答案 1 :(得分:2)

您可以使用imagettfbbox()找出文本的尺寸(以像素为单位),然后使用它作为参考在文本下方绘制线条。我玩过它,这是我的尝试:

function createImage($text){

  putenv('GDFONTPATH=' . realpath('.'));

  $text = wordwrap($text, 40, "\n");  
  $padding = 10;                         // padding around text
  $uOffs = 2;                            // distance between the line and the text above it
  $uHeight = 1;                          // height of the under-line
  $lines = explode("\n", $text);         // split text in lines
  $lineLengths = array();                // will store length of each line
  $textSize = 11;
  $font = 'arialbd.ttf';

  // bounding box of all text
  $textBoundingBox = array_map('abs', imagettfbbox($textSize, 0, $font, $text));

  list($blx, $bly, $brx, $bry, $trx, $try, $tlx, $tly) = $textBoundingBox;

  // calculate image dimensions based on the bounding box data
  $x = max($brx, $trx) + ($padding * 4);
  $y = max($blx, $bly) + ($padding * 4);

  $img = imagecreatetruecolor($x, $y);

  // determine length of each line of text
  foreach($lines as $i => $line){
    $box = imagettfbbox($textSize, 0, $font, $line);
    $lineLengths[$i] = max($box[2], $box[4]);
  }

  $white = imagecolorallocate($img, 255, 255, 255);
  $grey = imagecolorallocate($img, 128, 128, 128);
  $black = imagecolorallocate($img, 0, 0, 0);
  $purple = imagecolorallocate($img, 97, 26, 139);
  imagefilledrectangle($img, 0, 0, $x - 1, $y - 1, $white);  

  imagettftext($img, $textSize, 0, $padding + min($tlx, $blx), $padding + min($tly, $bly), $purple, $font, $text);

  // starting Y position of the under-line
  $uY = $padding +  min($tly, $bly);

  // underline text...
  foreach($lineLengths as $length){
    imagefilledrectangle($img, $padding + min($tlx, $blx), $uY + $uOffs, $padding + $length, $uY + $uOffs + $uHeight, $purple);
    $uY += 19;
  }  

  header('Content-Type: image/png');
  imagepng($img);
  imagedestroy($img);
}

结果:

enter image description here

使用Unicode字符会更加灵活,因为您可以控制行高,以及相对于其上方文本的位置。