There's a bit of an annoyance with measuring font sizes and
drawing boxes around text. When fonts are measured using
ImageTTFbbox, the correct vertical height is returned. That
is, the measurement of the phrase "Hanging" will
be from the top of the "H" to the bottom of the
"g".
The problem is that functions like imageTTFtext align with
the "line" of the text - that is, in the phrase
"Hanging", the alignment is below the
"H", not the bottom of the "g". That
means that if you draw a rectangle behind your text, it'll
be incorrectly aligned because the hanging "g"
will be outside the box.
For example, this doesn't work as you might expect (because
the "g" hangs below the box):
<?php
// Get the size of the font box
$textbox = imageTTFBbox($size, 0, $font, 'Hanging');
$textwidth = abs($textbox[4] - $textbox[0]);
$textheight = abs($textbox[5] - $textbox[1]);
// Now draw a rectangle on the image
$colour = ImageColorAllocate($im, 100, 100, 100);
imagefilledrectangle($im, $x, $y - $textheight, $x +
$textwidth, $y, $colour );
// Now draw the text
$black = ImageColorAllocate($im, 0, 0, 0);
ImageTTFText($image['resource'], $size, 0, $x, $y, $black,
$font, 'Hanging');
?>
It also seems that the rectangle in the above example is
located 1 pixel to the left of the text.
I haven't found a way to resolve this problem correctly.
Instead, I have enlarged the rectangle and then put the text
into it. I don't think this will work absolutely correctly
for all fonts, so it's not exactly a perfect solution.
However, it's better than nothing! Here is a snippet of it:
<?php
$enlargex = $textwidth * 0.08;
$enlargey = $textheight * 0.1;
$enlargey2 = $textheight * 0.5;
// Now draw a rectangle on the image
$colour = ImageColorAllocate($im, 100, 100, 100);
imagefilledrectangle($im, $x - $enlargex, $y - $textheight -
$enlargey, $x + $textwidth + $enlargex, $y + $enlarge2,
$colour );
?>
----
Server IP: 194.145.210.3
Probable Submitter: 83.67.131.199
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- https://master
.php.net/note/edit/74686
Del: integrated -- h
ttps://master.php.net/note/delete/74686/integrated
Del: useless -- http
s://master.php.net/note/delete/74686/useless
Del: bad code -- htt
ps://master.php.net/note/delete/74686/bad+code
Del: spam -- https:/
/master.php.net/note/delete/74686/spam
Del: non-english --
https://master.php.net/note/delete/74686/non-english
Del: in docs -- http
s://master.php.net/note/delete/74686/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/74686
Reject -- https://mast
er.php.net/note/reject/74686
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|