note 85266 added to function.imagettfbbox
| From: | beosman at gmail dot com | Date: | Fri, 22 Aug 2008 16:00:40 +0000 |
| Subject: | note 85266 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143753@lists.php.net to get a copy of this message | ||
I was viewing the code for calculate the box of a text for a given font but I do not found one that
works fine with different angles from zero, so I have made a function simpler than above:
<?php
function calculateTextBox($text,$fontFile,$fontSize,$fontAngle) {
$rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
return array(
"left" => abs($minX),
"top" => abs($minY),
"width" => $maxX - $minX,
"height" => $maxY - $minY,
"box" => $rect
);
}
?>
With this function you can center an angled string in any image:
<?php
$mystring = "Hello world!";
$imgWidth = 300;
$imgHeight = 150;
$image = imagecreate($imgWidth,$imgHeight);
imagefill($image,imagecolorallocate($image,200,200,200));
$box = calculateTextBox($mystring,"./Verdana.ttf",15,45);
$color = imagecolorallocate($image,0,0,0);
imagettftext($image,
15,
45,
$box["left"] + ($imgWidth / 2) - ($box["width"] / 2),
$box["top"] + ($imgHeight / 2) - ($box["height"] / 2),
$color,
"./Verdana.ttf",
$mystring);
header("Content-Type: image/x-png");
imagepng($im);
imagedestroy($im);
?>
----
Server IP: 130.206.1.5
Probable Submitter: 217.127.83.93
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- https://master.php.net/note/edit/85266
Del: integrated -- https://master.php.net/note/delete/85266/integrated
Del: useless -- https://master.php.net/note/delete/85266/useless
Del: bad code -- https://master.php.net/note/delete/85266/bad+code
Del: spam -- https://master.php.net/note/delete/85266/spam
Del: non-english -- https://master.php.net/note/delete/85266/non-english
Del: in docs -- https://master.php.net/note/delete/85266/in+docs
Del: other reasons-- https://master.php.net/note/delete/85266
Reject -- https://master.php.net/note/reject/85266
Search -- https://master.php.net/manage/user-notes.php