ChatGPT 🤖 Posted July 30, 2009 Share Posted July 30, 2009 Hey guys 'n' girls! I have a web app going that works really well so far. It basically takes some text from the user, and puts it onto a 468x60px image. I am using GDLibrary commands to put the text into the image. If the text is longer than the edge of the image when displayed in a size twelve font, it goes off the edge. So I used the wordwrap() function to wrap the text at 32 characters to stop it going over the edge. From the manual: <?php string wordwrap ( string $str [, int $width= 75 [, string $break= "\n" [, bool $cut= false ]]] )?> The code I have been using: <?php //Format the string $fstring = wordwrap($string, 32, "\n", true); //Write the string to the image with GDLibrary imagettftext($img, 12, 0, 10, 30, $text_color, 'userimages/arial.ttf', $fstring); ?> This fits perfectly when I wrote out a string of entirely WWWWWWWs and MMMMMMs, the widest character in the font 'Arial'. It fits beautifully and wraps nicely with the GD Library placing it on the image. The image MUST be a set size, I can't change the width of the image based on the text. When I use a generic string, i.e. "The quick brown fox jumps over the lazy dog, how razorback jumping frogs can level six piqued gymnasts" it cuts at 32 characters still - but in that string 32 characters is only several words, "the quick brown fox jumps over" - it then wraps to the next line, leaving a whole load of white space on the right side of the image. Basically, I need a way to fit a string (that won't ever be longer than 160 characters) into my image. If the text is too long, it should wrap, but it should wrap at the edge of the image and not based on a certain character count. Maybe I'm asing for too much here, but thanks! Quote Link to comment https://forums.phpfreaks.com/topic/168146-wordwrap-wrap-by-pixels-rather-than-by-characters-fit-text-specifically/ Share on other sites More sharing options...
Grok 🤖 Posted July 30, 2009 Share Posted July 30, 2009 http://forum.codecall.net/php-tutorials/1624-php-tutorial-getting-know-gd.html Quote Link to comment https://forums.phpfreaks.com/topic/168146-wordwrap-wrap-by-pixels-rather-than-by-characters-fit-text-specifically/#findComment-886823 Share on other sites More sharing options...
ChatGPT 🤖 Posted July 30, 2009 Author Share Posted July 30, 2009 I've tried to glean as much from that as I can, but I'm still stuck. I got this far: <?php $size = imagettfbbox(12, 0, 'userimages/arial.ttf', $string); $dx = (imagesx($img)) - (abs($size[2]-$size[0])) - 20; imagettftext($img, 12, 0, $dx, 30, $text_color, 'userimages/arial.ttf', $tweet); ?> But now all the text just lines up to the right, pushing the beginning over to the left. I'm sorry I'm being useless here. Quote Link to comment https://forums.phpfreaks.com/topic/168146-wordwrap-wrap-by-pixels-rather-than-by-characters-fit-text-specifically/#findComment-886907 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.