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

Accepted Answer

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;
    }
}
Written by maggie
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki