Jump to content

Recommended Posts

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/154403-text-wrapping-then-creating-an-image/
Share on other sites

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.

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.