【问题标题】:Word wrap code cuts the text at a random point自动换行代码在随机点剪切文本
【发布时间】:2019-03-12 13:30:43
【问题描述】:

我使用 PHP 和 GD 库编写了一个代码,该代码接收一个字符串作为输入,并将它分成几行,以便它可以放入图像中。问题在于,根据我输入的文本,它会在随机点停止。例如,使用以下文本作为输入:

Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat。 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 Exceptioneur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum。

输出图像是这样的:

我的代码是这样的:

<?php
function createStory($content){
    $text = $content;
    $jpg_image = imagecreatefromjpeg('imagebuilder/footage/story1.jpg');
    $white = imagecolorallocate($jpg_image, 255, 255, 255);
    $font_path = 'Arial.ttf';
    $words = explode(" ",$text);
    $proccessedtext = "";
    $line = "";
    $line .= $words[0] . " ";
    for($i = 1; $i < count($words); $i++){
        $bbox = imagettfbbox(25, 0, $font_path, $line);
        $width = $bbox[4]-$bbox[0];
        if($width<700){
            $line .= $words[$i] . " ";
        }else{
            $proccessedtext .= $line . " \n".$words[$i]. " ";
            $line = "";
        }
    }
    imagettftext($jpg_image, 25, 0, 75, 600, $white, $font_path, $proccessedtext);
    imagejpeg($jpg_image, "imagebuilder/created/readyStory.jpg");
    imagedestroy($jpg_image);
    return("/imagebuilder/created/readyStory.jpg");
}
?>

我的代码有错误还是库中的错误?

【问题讨论】:

    标签: php gd word-wrap


    【解决方案1】:

    很简单:注意$processedText 在超过最大宽度之前不会收到$line 的内容!因此,在任何给定时间,它只会收到一整行加上溢出的一个单词。因此,如果您的剩余文本不超过当前行一个额外的单词,则仍有剩余文本需要处理。尝试在 for 循环后直接添加$processedText .= $line;

    <?php
    function createStory($content){
        $text = $content;
        $jpg_image = imagecreatefromjpeg('imagebuilder/footage/story1.jpg');
        $white = imagecolorallocate($jpg_image, 255, 255, 255);
        $font_path = 'Arial.ttf';
        $words = explode(" ",$text);
        $proccessedtext = "";
        $line = "";
        $line .= $words[0] . " ";
        for($i = 1; $i < count($words); $i++){
            $bbox = imagettfbbox(25, 0, $font_path, $line);
            $width = $bbox[4]-$bbox[0];
            if($width<700){
                $line .= $words[$i] . " ";
            }else{
                $proccessedtext .= $line . " \n".$words[$i]. " ";
                $line = "";
            }
        }
        $processedText .= $line;
        imagettftext($jpg_image, 25, 0, 75, 600, $white, $font_path, $proccessedtext);
        imagejpeg($jpg_image, "imagebuilder/created/readyStory.jpg");
        imagedestroy($jpg_image);
        return("/imagebuilder/created/readyStory.jpg");
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2014-02-03
      • 2017-09-05
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多