And*_*iga 2 php gd imagettftext
使用imagettftextPHP GD库中的函数,只能绘制左对齐到图像上的文本.至少看起来似乎,也许我错过了一些东西.
如何控制绘制到图像上的文本的对齐方式?即左/中/右(/对齐 - 没必要,但有用)
我应该提一下,当绘制的文本包含多行时,对齐是可见的(例如"bla bla bla/n和另一行bla.").
与imagettftext()你没有写入某种"盒子"但只是在给定的协调员.要正确地对文本进行aling,你必须计算正确的坐标,使它"看起来像"它是右对齐或居中的.
为此,您可以使用imagettfbox()获取文本的大小 - 其余的是简单的数学:
[textarea-width]-[textwidth]到你的X坐标([textarea-width]-[textwidth]) / 2到你的X坐标(*textarea =您要在图像中写入文本的区域 - 应该知道它的大小)
您可以使用stil / gd-text类。免责声明:我是作者。
<?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");
Run Code Online (Sandbox Code Playgroud)
示范:

| 归档时间: |
|
| 查看次数: |
9126 次 |
| 最近记录: |