I'm programming a script that inserts a text on an image, it works but not entirely, ie if a user adds a line break in the textarea is fine, but if all the text is on one line looks bad . This is my code
$str="this is a string inserted by an user";
$img_width= 500;
$img_height= 500;
$font_size = 1;
$txt_max_width = intval(0.6 * $img_width);
do {
$font_size++;
$p = imagettfbbox($font_size,0,$font,$str);
$txt_width=$p[2]-$p[0];
} while ($txt_width <= $txt_max_width);
$y = $img_height * 0.4;
$x = ($img_width - $txt_width) / 2;
$white = imagecolorallocate($img, 255, 255, 255);
imagettftext($img, $font_size, 0, $x, $y, $white, $font, $str);
imagepng($img);
imagedestroy($img);
Look at this for understand http://app.xskarx.com
PS: sorry for my bad english