I’m using imagettftext to make a bar graph and at the top of each bar I want to put the value.
I have the following variables for each bar (which are really rectangles)
$x1
$y1
$x2
$y2
$imagesx
$imagesy
$font_size
Also, The fontsize should decrease as the string length increases.

Do it like this. Remember to place the font file “arial.ttf” in current directory:
It uses
imagettfbboxfunction to get width of the text, then loops over the font size to get correct size, centers it and displays it.So, it outputs the following: