我正在一个小的应用程序在PHP中,这是把文字图像。我用imagettftext获得了它。我也可以使用这个功能http://php.net/manual/en/function.imagettftext.php#89505将它放到具有特定宽度的imagettfbox。我唯一的问题是,我想要文本像在图像上对齐到框的右侧PHP文本图像,文本对齐
0
A
回答
0
这是代码,但我不需要将整个框对齐到X轴上的右侧,但是只有文字。你可以像它一样的文字是HTML
<p style="width:300px;text-align:right">This is the text to be wrapped and aligned to the right</p>
function wrap($fontSize, $angle, $fontFace, $string, $width){
$ret = "";
$arr = explode(' ', $string);
foreach ($arr as $word){
$teststring = $ret.' '.$word;
$testbox = imagettfbbox($fontSize, $angle, $fontFace, $teststring);
if ($testbox[2] > $width){
$ret.=($ret==""?"":"\n").$word;
} else {
$ret.=($ret==""?"":' ').$word;
}
}
return $ret;
}
function textToImage(){
$pluginDir = plugin_dir_path(__FILE__);
$image = imagecreatefrompng($pluginDir . 'resources/img/image.png');
$color = imagecolorallocate($image, 38, 64, 142);
$font_path = $pluginDir . 'resources/font/DroidSans.ttf';
$line = "This is the text to be wrapped and aligned to the right";
imagettftext($image, 35, 0, 300, 600, $color, $font_path, wrap(18, 0, $font_path, $line, 300));
imagepng($image, $pluginDir."resources/img/result.png");
imagedestroy($image);
}
+0
任何帮助家伙?我越来越绝望了。 – Jamalissimo
0
您可以使用stil/gd-text类。它为你做了正确的对齐和文字包装。
<?php
use GDText\Box;
use GDText\Color;
$pluginDir = plugin_dir_path(__FILE__);
$image = imagecreatefrompng($pluginDir . 'resources/img/image.png');
$textbox = new Box($image);
$textbox->setFontSize(35);
$textbox->setFontFace($pluginDir . 'resources/font/DroidSans.ttf');
$textbox->setFontColor(new Color(38, 64, 142));
$textbox->setBox(
300, // distance from left edge
600, // distance from top edge
500, // textbox width
500 // textbox height
);
// now we have to align the text horizontally and vertically inside the textbox
$textbox->setTextAlign('top', 'right');
// it can wrap multilined text
$textbox->draw("This is the text to be wrapped and aligned to the right");
imagepng($image, $pluginDir."resources/img/result.png");
imagedestroy($image);
示范:
相关问题
- 1. 对齐文本与图像
- 2. 对齐文本和图像
- 3. UL图像文本对齐
- 4. 对齐文本图像(CSS)
- 5. 将图像与文本视图中的文本对齐
- 6. 文本对齐到图像宽度
- 7. 图像对齐文本-CSS/HTML
- 8. 将文本对齐下面的图像
- 9. 基于文本对齐bg图像
- 10. 使用文本对齐定位图像
- 11. HTML和CSS对齐图像和文本
- 12. 多个图像下的文本对齐
- 13. GWT PushButton图像/文本对齐
- 14. Div图像和文本对齐
- 15. 如何将文本与图像对齐?
- 16. 如何将文本与图像对齐
- 17. 对齐问题(图像+文本)
- 18. 使用svg图像对齐文本
- 19. CSS对齐方式,图像和文本
- 20. 在Outlook中对齐图像和文本
- 21. top将文本与图像对齐
- 22. 在文本框旁边对齐图像
- 23. 文本和图像并排对齐
- 24. 将图像与文本对齐
- 25. 顶格内对齐文本和图像
- 26. 使用浮动图像对齐文本
- 27. 沿着图像垂直文本对齐
- 28. 将图像和文本与GridView对齐
- 29. 将文本和图像相邻对齐
- 30. 图像,然后对齐文本右
需要看到你的代码示例。放置TFF文本框将基于X,Y值。请编辑并添加您的代码。 – Twisty