使用具有多行的 imagettftext 函数?
我正在创建透明文本 -> png 图像与 php 到目前为止一切顺利。唯一的问题是我希望能够由于固定宽度而使文本自动换行。或者能够在文本中插入断线。有没有人有任何经验这样做?这是我的代码...
<?php
$font = 'arial.ttf';
$text = 'Cool Stuff! this is nice LALALALALA LALA HEEH EHEHE';
$fontSize = 20;
$bounds = imagettfbbox($fontSize, 0, $font, $text);
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$im = imagecreatetruecolor($width, $height);
imagealphablending($im, false);
imagesavealpha($im, true);
$trans = imagecolorallocatealpha($im, 255, 255, 255, 127);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $black);
imagefilledrectangle($im, 0, 0, $width, $height, $trans);
// Add the text
imagettftext($im, $fontSize, 0, 0, $fontSize-1, $grey, $font, $text);
imagepng($im, "image.png");
imagedestroy($im);
?>
I'm creating transparent text -> png images with php and so far so good. The only problem is that I want the ability to have the text word wrap due to a fixed width.. Or alternatively be able to insert breaklines into the text. Has anyone had any exp doing this? here is my code...
<?php
$font = 'arial.ttf';
$text = 'Cool Stuff! this is nice LALALALALA LALA HEEH EHEHE';
$fontSize = 20;
$bounds = imagettfbbox($fontSize, 0, $font, $text);
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$im = imagecreatetruecolor($width, $height);
imagealphablending($im, false);
imagesavealpha($im, true);
$trans = imagecolorallocatealpha($im, 255, 255, 255, 127);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $black);
imagefilledrectangle($im, 0, 0, $width, $height, $trans);
// Add the text
imagettftext($im, $fontSize, 0, 0, $fontSize-1, $grey, $font, $text);
imagepng($im, "image.png");
imagedestroy($im);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
Try this:
只需在空格上分解文本即可获得单词数组,然后通过循环单词数组开始构建线条,通过 imagettfbbox 测试每个新单词的添加,看看它是否创建了超过您设置的 maxwidth 的宽度。如果是,则在新行上开始下一个单词。我发现更容易简单地创建一个添加了特殊换行符的新字符串,然后再次分解该字符串以创建一个行数组,每行都将单独写入最终图像。
像这样:
其中管道字符是换行符。
Simply explode the text on spaces to get an array of words, then start building lines by looping through the words array, testing the addition of each new word via imagettfbbox to see if it creates a width that exceeds the maxwidth you set. If it does, start the next word on a fresh new line. I find it easier to simply create a new string with special line breaks characters added, and then just explode that string again to create an array of lines, each of which you will write onto the final image separately.
Something like this:
Where the pipe character is the line break character.
在发布的所有答案中,我最喜欢麻烦中的天才,但它只是增加了一个每 15 个字符换行一次,而不是像在现代文字处理器中那样让文本“流动”,行长可变,具体取决于字体选择和行长。使用哪些字符(例如,小写 L 比大写 W 占用的水平空间更少——l 与 W)。
我想出了一个解决方案,已在 https://github.com/andrewgjohnson/linebreaks4imagettftext 开源发布
要使用,您只需更改:
To:
这是 before & 的示例。之后是较长的文本:
Of all the answers posted I liked Genius in trouble's the best but it just adds a linebreak every 15 characters rather than letting the text "flow" as it would in a modern word processor with variable line lengths depending on font choice & which characters are used (e.g. lowercase L takes less horizontal space than uppercase W--l vs. W).
I came up with a solution which I've released as open source at https://github.com/andrewgjohnson/linebreaks4imagettftext
To use you would simply change:
To:
Here is an example of the before & after with a longer piece of text:
如果您的字符串没有任何空格,您可以尝试以下操作:
If your string not have a any space you can try this :