#### BLOG

Tags

Main >> Web Development

# Text on an arc with PHP & GD

This is a simple function that calculates the position of each letter and prints the text letter by letter onto the given image using the imagettftext function.

`function imagettftextarc(\$image, \$size, \$angle, \$x, \$y, \$r, \$color, \$fontfile, \$text, \$dir=false){    \$sbox=imagettfbbox(\$size, 0, \$fontfile, ' ');    \$sbox=(\$sbox-\$sbox)*0.1;    \$angle=\$angle*M_PI/180;    foreach(preg_split('//u', \$text) AS \$t){        \$px=\$x+\$r*cos(\$angle);        \$py=\$y+\$r*sin(\$angle);        \$dirangle=(360-(M_PI/2+\$angle)*180/M_PI+(\$dir?180:0))%360;        imagettftext(\$image, \$size, \$dirangle, \$px, \$py, \$color, \$fontfile, \$t);        \$box=imagettfbbox(\$size, 0, \$fontfile, \$t);        \$dx=\$box-\$box;        \$da=abs(asin((\$dx+\$sbox)/\$r));        if(\$dir){            \$angle-=\$da;        }else{            \$angle+=\$da;        }    }}`

The paramaters are as follows:

\$image: The image to be printed on
\$size: Font size
\$angle: Starting angle in degrees. 0 points to 3 o'clock
\$x, \$y: The center of the circle