I am setting up dynamic forum signature images for my users and I want to be able to put their username on the image. I am able to do this just fine, but since usernames are different lengths and I want to right align the username, how can I go about doing this when I have to set x & y coordinates.
$im = imagecreatefromjpeg("/path/to/base/image.jpg");
$text = "Username";
$font = "Font.ttf";
$black = imagecolorallocate($im, 0, 0, 0);
imagettftext($im, 10, 0, 217, 15, $black, $font, $text);
imagejpeg($im, null, 90);
This will work...............
Use the imagettfbbox function to get the width of the string and then subtract that from the width of the image to get the starting x-coordinate.
Pre-calculate the size of the user's name using imagettfbbox().
From the width you get from there, you can then deduct the x position at which your text needs to start.
I enhanced sujithayur code, and created function which allows all aligns (left, center, right) & (top, center, middle) and its combinations. It also uses text shadow.
You can use stil/gd-text class. Disclaimer: I am the author.
You can also draw multilined text. Just use
\n
in the string passed todraw()
method. Example generated with this class: