PHP GD ttftext center alignment

后端 未结 2 969
萌比男神i
萌比男神i 2021-02-06 11:33

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 fol

2条回答
  •  庸人自扰
    2021-02-06 11:58

    Do it like this. Remember to place the font file "arial.ttf" in current directory:

     0)
            {
                $drawsize = $size - 1;
                $drawX = $x1 + $lastdifference / 2;
                break;
            }
            $lastdifference = $maxwidth - $width;
        }
        $size--;
        imagettftext($im, $drawsize, 0, $drawX, $y1 - 2, $allocatedcolor, $font, $text);
    }
    
    // Output to browser
    header('Content-type: image/png');
    
    imagepng($im);
    imagedestroy($im);
    ?>
    

    It uses imagettfbbox function 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:

    alt text

提交回复
热议问题