2011-08-03 88 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])); 
     } 
    } 
}