我正在一個小的應用程序在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