Jump to content

test line length for wrapping


graves_it

Recommended Posts

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 http://www.anotherlinks.com/";
    
    $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 http://www.anotherlinks.com/

 

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
http://www.anotherlinks.com/

 

Link to comment
Share on other sites

First make sure you're not doing this for all the wrong reasons:

http://www.webdesignerwall.com/tutorials/word-wrap-force-text-to-wrap/

 

I can't spend the time showing you exactly, but I can give you the general idea. If you use a fixed width font you can figure out the exact image pixel-to-character ratio. In other words, "for every 100 image pixels I have 15 characters" or whatever it turns out to be.

 

Then you use this:

http://www.php.net/manual/en/function.getimagesize.php

 

To figure out the width of the image. Now you know how many characters of width you can have for that image. Then you create some logic to split the overall string up into the dividing points you want wrapping at (spaces and dashes in your example) and put this into an array.

 

You then iterate over the array, printing one array element at a time while incrementing a width counter with strlen() on the element. After each print (or echo), so within your while or foreach loop, you use PHP's modulus operator to check and see if the width counter divided by the character width you figured out for your image (using the ratio) has a remainder of less than 0. Ideally, combine the modulus operator with a ternary operator in one expression: IF the remainder is 0 or less, print a "<br />", else print nothing. That will wrap it at the right spot. Something like this inside your loop:

 

$output .= ($counter % 15 < 1) ? '<br />' : '';

 

Here's the stuff you need to use to do this:

http://php.net/manual/en/function.strlen.php

Modulus: http://php.net/manual/en/language.operators.arithmetic.php

 

If you haven't figured it out already later and I have time I'll see if I can post some psuedo code since I guess this format may be hard to follow.

Link to comment
Share on other sites

In that case the method would be identical except you'd use \n or \r in place of the <br />. The concept is exactly the same.

 

If you want to surrender some control over this process (that's what PHP is all about!) you may also have an easier time using this:

 

http://php.net/manual/en/function.wordwrap.php

 

Both my solution and the built-in function would work perfectly in an HTML or non-HTML (gd, imagemagick) context. The first part of my solution for figuring out the pixel-to-character ratio would apply in either case.

Link to comment
Share on other sites

Here is what I have so far, still not working but it is an attempt to do what was said in the previous suggestions. Any ideas?

$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 http://www.anotherlinks.com/";

$string_chunks = explode(' ', $text);

// Takes the split and recombine
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;
    //wrap based on image size
    if(strlen($chunk) > $image_width) {
        // if there is already a word in the current line make the first bit $imagewidth - current line width
        if ($start_x > $image_width -10) {
            $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;
        }
        $image_width = $image_width - 10;
        $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;
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.