2011-08-03 42 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

mulitibyte文字の問題を解決するために$ text [$ i]をmb_substr($ text、$ i、1)に置き換えてください。 – 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

これはしばらく前から回答されていたことは分かっていますが、文字間隔があり、角度オフセットを維持した解決策が必要でした。

私はこれを達成するradziのコードを変更:

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

だけ(「E」または「A」のような)特別な文字といくつかのトラブルを避けるために(最高で)pidaliaの答えを完了するために、

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])); 
     } 
    } 
} 
関連する問題