PHP:测试换行的行长度
如何测试块是否超出图像大小并将该文本换行到下一行。不确定我的 if 语句是否正确地做到了这一点。
$text="Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem http://somelongurl.com/then-we-make-it-super-long-with-some-more/ Lorem Ipsum Lorem Ipsum Lorem Ipsum";
$string_chunks = explode(' ', $text);
foreach ($string_chunks as $chunk) {
if($end_x + $chunk > $image_width){
$start_x = 5;
$start_y += 20;
}
$coords = imagettfbbox($fontsize, $angle, $font, $chunk);
$end_x = $coords[0] + $coords[4] + 10;
$color_to_draw = is_a_url($chunk) ? $linkcolor : $black;
imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk);
$start_x += $end_x;
}
通过这段代码,我得到:
Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem
http://somelongurl.com/then-we-make-it-super-long-with-some-more/
Lorem Ipsum Lorem Ipsum Lorem Ipsum
我想要发生的事情是这样的:
Lorem Ipsum Lorem Ipsum Lorem Ipsum
Lorem http://somelongurl.com/then-we
-make-it-super-long-with-some-more/
Lorem Ipsum Lorem Ipsum Lorem Ipsum
How can I test if a chunk exceeds the image size and wrap that text to the next line. Not sure if I am even doing this correctly with my if statement.
$text="Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem http://somelongurl.com/then-we-make-it-super-long-with-some-more/ Lorem Ipsum Lorem Ipsum Lorem Ipsum";
$string_chunks = explode(' ', $text);
foreach ($string_chunks as $chunk) {
if($end_x + $chunk > $image_width){
$start_x = 5;
$start_y += 20;
}
$coords = imagettfbbox($fontsize, $angle, $font, $chunk);
$end_x = $coords[0] + $coords[4] + 10;
$color_to_draw = is_a_url($chunk) ? $linkcolor : $black;
imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk);
$start_x += $end_x;
}
With this code I get:
Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem
http://somelongurl.com/then-we-make-it-super-long-with-some-more/
Lorem Ipsum Lorem Ipsum Lorem Ipsum
What I would like to happen is something like:
Lorem Ipsum Lorem Ipsum Lorem Ipsum
Lorem http://somelongurl.com/then-we
-make-it-super-long-with-some-more/
Lorem Ipsum Lorem Ipsum Lorem Ipsum
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
wordwrap
并将第四个参数$cut
作为true
传递给它,以强制 URL 被破坏。实例。
Use
wordwrap
and pass it the fourth parameter,$cut
, astrue
to force the URL to be broken.Live example.
我想我知道你想要实现什么。我没有测试下面的代码,所以它可能需要一些改进和重新设计/测试。但它应该给你一个很好的起点
i guess i know what you want to achieve. i've didn't test the code below, so it could need some polish and re-thing / testing. but it should give you a good startpoint
一个主要错误,$end_x + (strlen($chunk)*$charPXsize) > $图像宽度
另一个错误,如果一行比$image_width长会发生什么?它会在下一行打印出来,但无论如何都太长了。
One main mistake, $end_x + (strlen($chunk)*$charPXsize) > $image_width
Another mistake, what will happen if a row is longer than $image_width? It'll print out on the next line, but will be too long for it anyway.
我所做的是迭代字符串,每次检查边界框是否太宽。如果是这样,请插入一个新行并继续,直到读完所有文本。然后将其全部写为一个大字符串...
现在,请注意,这不会以不同的方式为您的链接着色...但是,您始终可以在其中添加一种检测方法来确定链接将被写入的确切位置,然后覆盖如果有的话用你的颜色...
What I've done is to iterate over the string, checking each time to see if the bounding box gets too wide. Then if it does, insert a new line and continue until you've consumed all of your text. Then write it all as one big string...
Now, note that this will not color your links differently... But, you could always add a detection method in there to determine the exact position that the link will be written, and overwrite it with your color if it's there...