0

I am trying to create a function that generates an rectangle with some text on it.

I have the following function:

<?php
function  create_image()
{
 $im = @imagecreate(190, 190)or die("Cannot Initialize new GD image stream");
 $background_color = imagecolorallocate($im, 255, 255, 255);
 imagerectangle($im, 5, 5, $_SESSION['lungime'], 5+$_SESSION['latime'], $black);

 for ($i=1, $k=0; $i<=$_SESSION['rand'] ; $i++, $k=$k+12)
 imagettftext($im, 12, 0,5+x/2-(strlen($_SESSION['rand'.$i]))/2, 12+$k, $black, $_SESSION['fontr'.$i], $_SESSION['rand'.$i]);

 imagepng($im,"image.png");
 imagedestroy($im);
}
?>

I want the text that is generated within the for loop to be centered in the rectangle. I can't do it with strlen because it returns the number of characters that the string has. How cand I do that?

What about generating a center-aligned text in a generated circle?

Please help me ! Thank you !

Cristina Ursu
  • 201
  • 2
  • 6
  • 18

1 Answers1

0

You can use PHP's imagettfbbox to get the dimensions of text you're printing in GD. Then it's just a bit of math to work out where to set the left point to.

chip
  • 599
  • 1
  • 9
  • 20