如何测试 block 是否超过图像大小并将该文本换行到下一行。不确定我是否使用 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
最佳答案
使用 wordwrap
并将第四个参数 $cut
传递给它作为 true
以强制断开 URL。
关于PHP:测试换行的行长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3609922/