3

我的问题是,如何在图像中的所有文字下划线?

代码:

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

结果 http://i.imgur.com/Jdr7HPy.png

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

4

2 回答 2

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);
于 2013-02-07T00:44:03.690 回答
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);
}

结果:

在此处输入图像描述

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

于 2013-02-07T01:50:19.607 回答