graves_it Posted August 31, 2010 Share Posted August 31, 2010 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/ Quote Link to comment Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 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. Quote Link to comment Share on other sites More sharing options...
graves_it Posted August 31, 2010 Author Share Posted August 31, 2010 I can't use css or html because I am outputting as an image with the imagettftext. So I am not sure the method you are using will work as it uses <br />. The image is a fixed width defined already with $image_width = 400; if that changes anything. Quote Link to comment Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 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. Quote Link to comment Share on other sites More sharing options...
graves_it Posted September 1, 2010 Author Share Posted September 1, 2010 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; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.