inspireddesign Posted April 16, 2009 Share Posted April 16, 2009 Hello all, I'm trying to create an image on the fly (which works) but for some reason the wordwarp() function doesn't work when passed to the imagestring() function. It works fine if the $newtest is echoed. Any ideas? <?php // Create a new image instance $im = imagecreatetruecolor(200, 200); $black = imagecolorallocate($im, 0, 0, 0); $white = imagecolorallocate($im, 255, 255, 255); // Make the background white imagefilledrectangle($im, 0, 0, 200, 200, $white); $text = "A very long woooooooooooord."; $newtext = wordwrap($text, 8, "\n", true); // die($newtext); // Load the gd font and write 'Hello' $font = imageloadfont('tahoma-font.gdf'); imagestring($im, $font, 0, 0, $newtext, $black); // Output to browser header('Content-type: image/gif'); imagepng($im); imagedestroy($im); ?> Quote Link to comment https://forums.phpfreaks.com/topic/154403-text-wrapping-then-creating-an-image/ Share on other sites More sharing options...
The Little Guy Posted April 16, 2009 Share Posted April 16, 2009 I don't believe you can use wordwrap on images... Try this: $text = "A very long woooooooooooord."; $newtext = substr_replace($text, "\n", 8, 0); Quote Link to comment https://forums.phpfreaks.com/topic/154403-text-wrapping-then-creating-an-image/#findComment-811851 Share on other sites More sharing options...
inspireddesign Posted April 16, 2009 Author Share Posted April 16, 2009 thanks for the reply but that did do it. Does it have to do with the header()? I wouldn't think so? Does anyone else have a suggestion? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/154403-text-wrapping-then-creating-an-image/#findComment-811902 Share on other sites More sharing options...
.josh Posted April 16, 2009 Share Posted April 16, 2009 First, decide whether you want your wordwrap to create a new line in the middle of a word or not. If you do NOT want it to start a new line in the middle of a word, set the 4th argument to 0 or false. If you DO want it to start a new line, regardless of whether the line length specified in the 2rd argument is in the middle of a word, set it to 1 or true. 2nd: imagestring does not recognize your newline characters. In order to achieve your wordwrap using it, after you wordwrap, you must explode at the \n and run imagestring for every array element from the explode, making sure to increment the y coord by your font size. There is an alternative method of doing this, with imagettftext, which does recognize line breaks. Read the documentation/user notes from the link, or you can check out this tutorial that makes use of it. Quote Link to comment https://forums.phpfreaks.com/topic/154403-text-wrapping-then-creating-an-image/#findComment-811920 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.