3

PHP GD ライブラリの関数を使用するimagettftextと、画像上に左揃えのテキストのみを描画できます。少なくともそう見えるでしょう、おそらく私は何かが欠けています。

画像に描画されたテキストの配置を制御するにはどうすればよいですか? つまり、左 / 中央 / 右 (/ 両端揃え - 必須ではありませんが、便利です)

描画されたテキストに複数の行が含まれている場合 (例: "bla bla bla/nAnd another line of bla.") に配置が表示されることに注意してください。

4

3 に答える 3

6

imagettftext()ある種の「ボックス」に書き込んでいるのではなく、指定された座標に書き込んでいます。テキストを適切に配置するには、正しい座標を計算して、右揃えまたは中央揃えに「見える」ようにする必要があります。

そのために、 を使用imagettfbox()してテキストのサイズを取得できます。残りは簡単な計算です。

  • -coordinate[textarea-width]-[textwidth]に右揃えに追加X
  • 中心に-座標([textarea-width]-[textwidth]) / 2に追加X

(* textarea = 画像内のテキストを書きたい領域 - サイズはわかっているはずです)

于 2012-11-05T13:58:49.867 に答える
5

You can use stil/gd-text class. Disclaimer: I am the author.

<?php
use GDText\Box;
use GDText\Color;

$img = imagecreatefromjpeg('image.jpg');

$textbox = new Box($img);
$textbox->setFontSize(12);
$textbox->setFontFace('arial.ttf');
$textbox->setFontColor(new Color(255, 255, 255));
$textbox->setBox(
    50,  // distance from left edge
    50,  // distance from top edge
    200, // textbox width
    100  // textbox height
);

// now we have to align the text horizontally and vertically inside the textbox
$textbox->setTextAlign('left', 'top');
// or like this:
// $textbox->setTextAlign('right', 'top');
// $textbox->setTextAlign('center', 'top');
// $textbox->setTextAlign('center', 'bottom');
// $textbox->setTextAlign('right', 'center');

// it accepts multiline text
$textbox->draw("text to write on picture\nanother line below");

Demonstration:

demo

于 2015-01-02T12:52:11.113 に答える
0

この関数は、x と y の位置座標を受け入れます。

    array imagettftext ( resource $image, 
                         float $size, 
                         float $angle, 
                         int $x, 
                         int $y, 
                         int $color, 
                         string $fontfile, 
                         string $text )

http://php.net/manual/en/function.imagettftext.phpをご覧ください。

于 2012-11-05T14:01:33.153 に答える