2011-08-03 90 views
6

有没有人用指定的字母间距绘制一个ttf字符串(imagettftext)的函数?php imagettftext字间距

我找不到任何内置的GD函数,所以我认为应该逐字逐句添加一些常量宽度。

也许某人有这样的功能:)

ps。最好的字体将是arial.ttf

回答

20
function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0) 
{   
    if ($spacing == 0) 
    { 
     imagettftext($image, $size, $angle, $x, $y, $color, $font, $text); 
    } 
    else 
    { 
     $temp_x = $x; 
     for ($i = 0; $i < strlen($text); $i++) 
     { 
      $bbox = imagettftext($image, $size, $angle, $temp_x, $y, $color, $font, $text[$i]); 
      $temp_x += $spacing + ($bbox[2] - $bbox[0]); 
     } 
    } 
} 

和呼叫:

imagettftextSp($image, 30, 0, 30, 30, $black, 'arial.ttf', $text, 23); 

功能参数顺序符合标准imagettftext参数顺序,最后一个参数是可选的$ spacing参数。如果未设置或传递的值为0,则不设置字距/字母间距。

+0

用mb_substr($ text,$ i,1)替换$ text [$ i]来克服多字节字符的问题 – Juergen

0

GD不支持字距调整,所以你必须手动完成。就我个人而言,我写了一个函数,分别写每个字母。现在我无法找到它,但它是沿着线的东西:

function drawText(&$image, $text, $fgColor, $font, $fgColor, 
        $fontSize = 14, $kerning = 0, $x = 0, $y = 0) { 
    $letters = explode('', $text); 

    foreach ($letters as $n => $letter) { 
     $bbox = imagettftext($image, $fontSize, 0, $x, $y, $fgColor, $font, $letter); 
     $x += $bbox[2] + $kerning; 
    } 
} 
-1

试试这个功能:

$image = imagecreatetruecolor(500,200); 
$text = "Text to print"; 
$text_color=imagecolorallocate($image,255,255,255); 
$font_size = 18; 
$space = 8; 
$font = "path_to_font/arial.ttf"; 
$x=20; 
$y=20; 
for ($i = 0; $i <strlen($text); $i++){ 
    $arr = imagettftext ($image, $font_size,0, $x, $y, $text_color, $font, $text{$i}); 
    $x = $arr[4]+$space; 
} 
imagejpeg($image); 
destroyimage($image); 
10

我知道这是回答了一段时间,但我需要一个解决方案,有字母间距,并保持角偏移。

我修改拉齐的代码来实现:

function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0) 
{   
    if ($spacing == 0) 
    { 
     imagettftext($image, $size, $angle, $x, $y, $color, $font, $text); 
    } 
    else 
    { 
     $temp_x = $x; 
     $temp_y = $y; 
     for ($i = 0; $i < strlen($text); $i++) 
     { 
      imagettftext($image, $size, $angle, $temp_x, $temp_y, $color, $font, $text[$i]); 
      $bbox = imagettfbbox($size, 0, $font, $text[$i]); 
      $temp_x += cos(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0])); 
      $temp_y -= sin(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0])); 
     } 
    } 
} 
+1

很高兴看到你用这个答案回馈给Stack Overflow社区。好的表演+1 – Fluffeh

5

只是为了完成pidalia的回答(这是最好的),以避免一些麻烦特殊字符(如“E”或“A”)

static function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0) { 
    if ($spacing == 0) { 
     imagettftext($image, $size, $angle, $x, $y, $color, $font, $text); 
    } else { 
     $temp_x = $x; 
     $temp_y = $y; 
     //to avoid special char problems 
     $char_array = preg_split('//u',$text, -1, PREG_SPLIT_NO_EMPTY); 
     foreach($char_array as $char) { 
      imagettftext($image, $size, $angle, $temp_x, $temp_y, $color, $font, $char); 
      $bbox = imagettfbbox($size, 0, $font, $char); 
      $temp_x += cos(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0])); 
      $temp_y -= sin(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0])); 
     } 
    } 
}