note 83248 added to function.imagettftext
| From: | ValentijndePagter at osu1 dot php dot net | Date: | Fri, 16 May 2008 17:13:34 +0000 |
| Subject: | note 83248 added to function.imagettftext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139449@lists.php.net to get a copy of this message | ||
If you're looking for easy text alignment, you need to use the imagettfbbox() command. When
given the correct parameters, it will return the boundaries of your to-be-made text field in an
array, which will allow you to calculate the x and y coordinate that you need to use for centering
or aligning your text.
A horizontal centering example:
<?php
$tb = imagettfbbox(17, 0, 'airlock.ttf', 'Hello world!');
?>
$tb would contain:
Array
(
[0] => 0 // lower left X coordinate
[1] => -1 // lower left Y coordinate
[2] => 198 // lower right X coordinate
[3] => -1 // lower right Y coordinate
[4] => 198 // upper right X coordinate
[5] => -20 // upper right Y coordinate
[6] => 0 // upper left X coordinate
[7] => -20 // upper left Y coordinate
)
For horizontal alignment, we need to substract the "text box's" width { $tb[2] or
$tb[4] } from the image's width and then substract by two.
Saying you have a 200px wide image, you could do something like this:
<?php
$x = ceil((200 - $tb[2]) / 2); // lower left X coordinate for text
imagettftext($im, 17, 0, $x, $y, $tc, 'airlock.ttf', 'Hello world!'); // write
text to image
?>
This'll give you perfect horizontal center alignment for your text, give or take 1 pixel. Have
fun!
----
Server IP: 213.249.64.162
Probable Submitter: 82.161.28.173
----
Manual Page -- http://www.php.net/manual/en/function.imagettftext.php
Edit -- https://master.php.net/note/edit/83248
Del: integrated -- https://master.php.net/note/delete/83248/integrated
Del: useless -- https://master.php.net/note/delete/83248/useless
Del: bad code -- https://master.php.net/note/delete/83248/bad+code
Del: spam -- https://master.php.net/note/delete/83248/spam
Del: non-english -- https://master.php.net/note/delete/83248/non-english
Del: in docs -- https://master.php.net/note/delete/83248/in+docs
Del: other reasons-- https://master.php.net/note/delete/83248
Reject -- https://master.php.net/note/reject/83248
Search -- https://master.php.net/manage/user-notes.php