im working with imagettfont() and i need a function that will center the text within an image, but also change the font size depending on the length of the text, and 5 need it to work with different fonts. so for example
the font is arial and the string is 6 characters i want the alignment in the middle of the image and the font size say 20 and then if its say 12 characters i want it in the middle of theimage but the font size only 10.
at the minute ive got something thats kind of works but its not very efficient because my coding skills arent very good, also it doesnt take into account different font widths. if any1 could help with even making the code more efficient it would be much appreciated.
// $text = first line of text, $text 2 is 2nd line, $fc = font colour, $font = font and $myImage = image name
function WriteText($text,$text2,$fc,$font,$myImage){
//ignore this bit, it makes sure something is in $text2 because im getting it from an array that parts of which may not exist
f (strlen($text2) < 1){
$text2 = "";
}
// this just sets the font colour
if ($fc == 1){
$font_colour = ImageColorAllocate($myImage,0,0,0);
} else {
$font_colour = ImageColorAllocate($myImage,255,255,255);
}
// if the length of the text is 10 or less font size is 20 and nudge is 5 (nudge is the amount to move back from the center for each character added
if (strlen($text) < 11){
$fsize = 20;
$nudge = 5;
} else {
$fsize = 15;
$nudge = 4;
}
//does the same for the second line
if (strlen($text2) < 11){
$fsize2 = 20;
$nudge2 = 5;
} else {
$fsize2 = 15;
$nudge2 = 4;
}
//this bit works out how many characters are in the line and moves the starting position left accordingly
if (strlen($text) > 0){
$pos = 140;
$x = 1;
while ($x < 13)
if (strlen($text) > $x){
$pos = ($pos - $nudge);
$x++;
}else{
$x = 13;
}
} else{
$pos = 100;
}
//same as above for for line 2
if (strlen($text2) > 0){
$pos2 = 140;
$x = 1;
while ($x < 13)
if (strlen($text2) > $x){
$pos2 = ($pos2 - $nudge2);
$x++;
}else{
$x = 13;
}
} else{
$pos2 = 100;
}
//writes the 2 lines
imagettftext($myImage, $fsize, 0, $pos, 120, $font_colour, $font, $text);
imagettftext($myImage, $fsize2, 0, $pos2, 140, $font_colour, $font, $text2);
}
how would i change it so say every few more letters it would add a set amount to the font size?