note 39720 added to function.imagettfbbox
| From: | jtoplandathivedotno at rn2 dot php dot net | Date: | Sun, 08 Feb 2004 19:26:17 +0000 |
| Subject: | note 39720 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64839@lists.php.net to get a copy of this message | ||
Finally managed to make a fixed version of imagettfbbox().
All angles returns correct values.
Except that imagettftext() returns different trackings (space between each character) when rotating.
<?php
// Set some test variables
$font = "d://www//tahoma.ttf";
$text = "Finally, I can center rotated text!";
$size = 20;
$angle = 20;
// Create an image and fill the background with lightgray
$image = imagecreatetruecolor(500, 400);
imagefill($image, 0, 0, hexdec("dddddd"));
// Make a cross to make it easier to analyze
imageline($image, 0, 0, imagesx($image), imagesy($image), hexdec("000000"));
imageline($image, imagesx($image), 0, 0, imagesy($image), hexdec("000000"));
// Run a fixed version of imagettfbbox()
$bbox = imagettfbbox_fixed($size, $angle, $font, $text);
// Make some text and center the text on the image.
// imagettftext() pivot is on lower left
imagettftext($image, $size, $angle, imagesx($image) / 2 - $bbox['width'] / 2,
imagesy($image) / 2 + $bbox['height'] / 2, hexdec("0000ff"), $font, $text);
// Show the image
imagepng($image);
function imagettfbbox_fixed($size, $angle, $font, $text)
{
// Get the boundingbox from imagettfbbox(), which is correct when angle is 0
$bbox = imagettfbbox($size, 0, $font, $text);
// Rotate the boundingbox
$angle = pi() * 2 - $angle * pi() * 2 / 360;
for ($i=0; $i<4; $i++)
{
$x = $bbox[$i * 2];
$y = $bbox[$i * 2 + 1];
$bbox[$i * 2] = cos($angle) * $x - sin($angle) * $y;
$bbox[$i * 2 + 1] = sin($angle) * $x + cos($angle) * $y;
}
// Variables which tells the correct width and height
$bbox['width'] = $bbox[0] + $bbox[4];
$bbox['height'] = $bbox[1] - $bbox[5];
return $bbox;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39720
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39720&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39720&report=yes
Search -- http://master.php.net/manage/user-notes.php