Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with using imagestring and aligning text

Status
Not open for further replies.

rye8261

Technical User
Jul 6, 2003
359
US
First let me say I'm not a developer by trade.

Now I'm trying to create a dynamic image that displays text in a thinking box. I'm trying to aling the text in the middle of the box and then expand out evenly if it goes onto seperate lines.

I've managed to get the text to display and go on multiple lines if needed but I don't know how to align it in the middle of the box.

You can see the box here:
Thanks for any help.

---------------------------------------
- Free General Web Directory, Free Web site submissions.
- Free General Web Directory, Free Web site submissions.
 
Is there a PHP question in there?

I think you'll be better off in the HTML forum:forum215

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I'd dynamically placing the text with PHP so of course it's a PHP question.

This is what I have so far that I'm working with:
Code:
function getx($img, $font, $text){
 	$image_width = imagesx($img);
	$string_width = imagefontwidth($font)*strlen($strFile);
	$string_x_position = ($image_width-$string_width)/2;
}
function gety($img, $font, $text){
	$image_height = imagesy($img);
	$string_height = imagefontheight($font);
	$string_y_position = ($image_height-$string_height)/2;
}


header("Content-type: image/png");
	$img    = imagecreatefrompng("./images/thoughtbox-" . $_GET["id"] . ".png");
	
	$font = 2;
	
	$strFile = $_GET['text'];
	$strQuote = wordwrap($strFile, 20, "\n");
		
	$string_y_position = 15;
	$ystring = 0;
	$delta_y = ImageFontHeight($font) + 2;
	
	$strings = explode("\n", $strQuote);
	
	foreach ($strings as $s) {
	$x = getx($img, $font, $strFile);
	$y = gety($img, $font, $strFile);
	
	 ImageString($img, $font, $x, $y+$ystring, $s, "#000000");
	   //imagestring($img, $font, 8, $y, $s, "#000000");
	   $ystring += $delta_y;
	}
	
	
	imagepng($img);
	imagedestroy($img);

---------------------------------------
- Free General Web Directory, Free Web site submissions.
- Free General Web Directory, Free Web site submissions.
 
I'm sorry, did not catch that from your first post. And i'm afraid i wont be much help. Someone here probably has more experience with the image creating functions in PHP.








----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I've gotten the x align to center, but I don't know how to align vertically in the box. Working on it :)

---------------------------------------
- Free General Web Directory, Free Web site submissions.
- Free General Web Directory, Free Web site submissions.
 
currently you START a multiline at the middle whereas you should be starting the multiline before the middle.

step 1.
count the number of line breaks you have in the wordwrapped string.

step 2
create an array of y positions. if the count above is even then the middle value can be 0 and each earlier value is a decrement of the $delta_y (vice versa for the other side). if the count is even then the middles need to straddle the center point by half $delta_y.

step 3
iterate over the array plotting the text values.

let us know if you need help coding these steps.
 
i was bored so adapted your code a bit. the image you are using is not symmetrical about the bubble so i use a square in this example to make it obvious how the centering works.

Code:
<?
//create a blank image

$img    = 	imagecreateTrueColor(200, 200);
//set some colors
$background = 	imagecolorallocate($img, 255, 0, 0);
$white = 	imagecolorallocate($img, 255, 255, 255);

$font = 	2;

//put some cross hatches in to determine visual centre
imageline (	$img, 
		0, 
		imagesy($img)/2, 
		imagesx($img), 
		imagesy($img)/2, 
		$white 
	);

imageline (	$img, 
		imagesx($img)/2, 
		0, 
		imagesx($img)/2, 
		imagesy($img), 
		$white 
	);

$strQuote = 	"Now is the time for all good men to come to the aid of the Party";
$strFile =	wordwrap($strQuote, 20, "\n");
$center = 	imagesy($img)/2;
$delta_y = 	ImageFontHeight($font); 
$strings = 	explode("\n", $strFile);
$count = 	count($strings);

$start = 	($count % 2 == 0)
		?	$center  - ($delta_y * ($count/2))
		:	$center  - ($delta_y * floor($count/2)) - ($delta_y/2) ; 

$correction = 	0;
for ($i=0; $i<$count; $i++):  
	        $ypos[$i] =	$start + $correction;
        	$correction = $correction + $delta_y;
endfor;

foreach($ypos as $key=>$y):
	$x = 	((imagesx($img) - (imagefontwidth($font) * strlen($strings[$key])) - 

$string_width )/2);

	ImageString($img, $font, $x, $y, $strings[$key], $white);
endforeach;
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>

hope it helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top