【问题标题】:Underline text using imagettftext使用 imagettftext 为文本添加下划线
【发布时间】:2013-02-07 00:39:01
【问题描述】:

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

代码:

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

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

【问题讨论】:

    标签: php freetype imagettftext


    【解决方案1】:

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

    您将需要一个循环或巧妙的数组合并来修改文本,

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

    【讨论】:

    • createImage("Stackoverflow");输出 S#x0332;t#0332;... 与您的循环,感谢您的快速回复
    • **&**#x0332; & 不见了:)
    • 啊,不错!更新了我的答案。
    【解决方案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 字符更灵活一些,因为您可以控制行高以及相对于其上方文本的位置。

    【讨论】:

      猜你喜欢
      • 2012-09-07
      • 2015-06-05
      • 2015-04-19
      • 1970-01-01
      • 2013-02-21
      • 2015-05-29
      • 2012-04-18
      • 2015-03-04
      • 2011-04-13
      相关资源
      最近更新 更多