PHP:测试换行的行长度

发布于 2024-09-17 05:49:26 字数 1078 浏览 5 评论 0原文

如何测试块是否超出图像大小并将该文本换行到下一行。不确定我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒中人 2024-09-24 05:49:26

使用 wordwrap 并将第四个参数 $cut 作为 true 传递给它,以强制 URL 被破坏。

实例

Use wordwrap and pass it the fourth parameter, $cut, as true to force the URL to be broken.

Live example.

尤怨 2024-09-24 05:49:26

我想我知道你想要实现什么。我没有测试下面的代码,所以它可能需要一些改进和重新设计/测试。但它应该给你一个很好的起点

<?php
$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) {
    $_start_bit = false;
    // before anything else check if chunk is url
    $color_to_draw = is_a_url($chunk) ? $linkcolor : $black; 
    // check if chunk is to long
    if(strlen($chunk) > $image_width) {
        // if there is allredy a word in the current line
        // make the first bit $imagewidth - current line width
        if ($start_x > 5) {
            $_start_bit = substr($chunk, 0, ($image_width - $start_x));
            $chunk = str_replace($_start_bit, "", $chunk);
        }
        $_chunkbits = wordwrap($chunk, $image_width, "\n", true);
        $_chunkbits = explode("\n", $_chunkbits);
        if($_start_bit) {
            array_unshift($_chunkbits, $_start_bit);
        }
        // loop bits and draw them
        foreach ($_chunkbits as $bit) {
            if($end_x + $bit > $image_width){
                $start_x = 5;
                $start_y += 20;
            }
            $coords = imagettfbbox($fontsize, $angle, $font, $bit);
            $end_x = $coords[0] + $coords[4] + 10;
            imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $bit);
            $start_x += $end_x;
        }
        unset($_chunkbits);
    } else {
        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;
        imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk);
        $start_x += $end_x;
    }
}

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

<?php
$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) {
    $_start_bit = false;
    // before anything else check if chunk is url
    $color_to_draw = is_a_url($chunk) ? $linkcolor : $black; 
    // check if chunk is to long
    if(strlen($chunk) > $image_width) {
        // if there is allredy a word in the current line
        // make the first bit $imagewidth - current line width
        if ($start_x > 5) {
            $_start_bit = substr($chunk, 0, ($image_width - $start_x));
            $chunk = str_replace($_start_bit, "", $chunk);
        }
        $_chunkbits = wordwrap($chunk, $image_width, "\n", true);
        $_chunkbits = explode("\n", $_chunkbits);
        if($_start_bit) {
            array_unshift($_chunkbits, $_start_bit);
        }
        // loop bits and draw them
        foreach ($_chunkbits as $bit) {
            if($end_x + $bit > $image_width){
                $start_x = 5;
                $start_y += 20;
            }
            $coords = imagettfbbox($fontsize, $angle, $font, $bit);
            $end_x = $coords[0] + $coords[4] + 10;
            imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $bit);
            $start_x += $end_x;
        }
        unset($_chunkbits);
    } else {
        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;
        imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk);
        $start_x += $end_x;
    }
}
过去的过去 2024-09-24 05:49:26
if($end_x + $chunk > $image_width){ 
    $start_x = 5; 
    $start_y += 20; 
} 

一个主要错误,$end_x + (strlen($chunk)*$charPXsize) > $图像宽度
另一个错误,如果一行比$image_width长会发生什么?它会在下一行打印出来,但无论如何都太长了。

if($end_x + $chunk > $image_width){ 
    $start_x = 5; 
    $start_y += 20; 
} 

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.

毁虫ゝ 2024-09-24 05:49:26

我所做的是迭代字符串,每次检查边界框是否太宽。如果是这样,请插入一个新行并继续,直到读完所有文本。然后将其全部写为一个大字符串...

$chunks = explode(' ', $text);
$wrappedText = '';
foreach ($chunks as $chunk) {
    $coords = imagettfbbox($fontsize, $angle, $font, $wrappedText.' '.$chunk);
    $width = $coords[2] - $coords[0];
    if ($width > $myMaxWidth) {
        $wrappedText .= "\n" . $chunk;
    } else {
        $wrappedText .= ' ' . $chunk;
    }
}
imagettftext(
    $im, 
    $fontsize, 
    $angle, 
    $start_x, 
    $start_y, 
    $color_to_draw, 
    $font, 
    $wrappedText
);

现在,请注意,这不会以不同的方式为您的链接着色...但是,您始终可以在其中添加一种检测方法来确定链接将被写入的确切位置,然后覆盖如果有的话用你的颜色...

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...

$chunks = explode(' ', $text);
$wrappedText = '';
foreach ($chunks as $chunk) {
    $coords = imagettfbbox($fontsize, $angle, $font, $wrappedText.' '.$chunk);
    $width = $coords[2] - $coords[0];
    if ($width > $myMaxWidth) {
        $wrappedText .= "\n" . $chunk;
    } else {
        $wrappedText .= ' ' . $chunk;
    }
}
imagettftext(
    $im, 
    $fontsize, 
    $angle, 
    $start_x, 
    $start_y, 
    $color_to_draw, 
    $font, 
    $wrappedText
);

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文