I was working on a PHP script yesterday for creating a couple of graphs which required lots of text in various places aligned to text on the same line. Some on the x axix and some on y. Finding no inbuilt function in gd on php, I created this simple function to align text vertically and horizontally as required based on the parameters passed to it.
function positiontextinimage($img, $font, $size, $color, $string, $x,$y, $horiz, $vert)
{
$bounds=imageftbbox ( $size , 0 , $font , $string);
switch($horiz){
case "left":
break;
case "right":
$x=$x-$bounds[4];
break;
case "center":
case "centre":
$x=$x-($bounds[4] -$bounds[0])/2;
break;
}
switch($vert){
case "top":
$y=$y-$bounds[5];
break;
case "bottom":
break;
case "middle":
$y=$y+($bounds[1]-$bounds[5])/2;
break;
}
imagefttext ( $img , $size , 0 , $x, $y, $color, $font , $string);
}
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.