7

指定された文字間隔で ttf 文字列 (imagettftext) を描画する関数を持っている人はいますか?

組み込みの GD 関数が見つからないので、文字ごとに一定の幅を追加する必要があると思います。

多分誰かがすでにそのような機能を持っています:)

ps。最適なフォントは arial.ttf です

4

6 に答える 6

27
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 の場合、カーニング/文字間隔は設定されません。

于 2011-08-04T06:37:58.453 に答える
12

私はこれがしばらく前に答えられたことを知っています、しかし私は文字間隔を持ちそして角度オフセットを維持する解決策を必要としていました。

これを実現するために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]));
        }
    }
}
于 2012-08-14T13:36:09.977 に答える
-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);
于 2011-08-03T13:00:47.353 に答える