note 93088 added to function.imagettfbbox
| From: | hkc at taiwan | Date: | Sat, 22 Aug 2009 07:57:08 +0000 |
| Subject: | note 93088 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159786@lists.php.net to get a copy of this message | ||
I want to output a bounding box of a text as an image straightly. I did like this:
<?php
$text = "<?php echo \"hello, world\"; ?>";
$font = "./arial.ttf";
$size = "60";
$bbox = imagettfbbox($size, 0, $font, $text);
$width = abs($bbox[2] - $bbox[0]);
$height = abs($bbox[7] - $bbox[1]);
$image = imagecreatetruecolor($width, $height);
$bgcolor = imagecolorallocate($image, 255, 255, 255);
$color = imagecolorallocate($image, 0, 0, 0);
$x = $bbox[0] + ($width / 2) - ($bbox[4] / 2);
$y = $bbox[1] + ($height / 2) - ($bbox[5] / 2);
imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor);
imagettftext($image, $size, 0, $x, $y, $color, $font, $text);
$last_pixel= imagecolorat($image, 0, 0);
for ($j = 0; $j < $height; $j++)
{
for ($i = 0; $i < $width; $i++)
{
if (isset($blank_left) && $i >= $blank_left)
{
break;
}
if (imagecolorat($image, $i, $j) !== $last_pixel)
{
if (!isset($blank_top))
{
$blank_top = $j;
}
$blank_left = $i;
break;
}
$last_pixel = imagecolorat($image, $i, $j);
}
}
$x -= $blank_left;
$y -= $blank_top;
imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor);
imagettftext($image, $size, 0, $x, $y, $color, $font, $text);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
----
Server IP: 140.112.2.5
Probable Submitter: 220.228.89.76
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- https://master.php.net/note/edit/93088
Del: integrated -- https://master.php.net/note/delete/93088/integrated
Del: useless -- https://master.php.net/note/delete/93088/useless
Del: bad code -- https://master.php.net/note/delete/93088/bad+code
Del: spam -- https://master.php.net/note/delete/93088/spam
Del: non-english -- https://master.php.net/note/delete/93088/non-english
Del: in docs -- https://master.php.net/note/delete/93088/in+docs
Del: other reasons-- https://master.php.net/note/delete/93088
Reject -- https://master.php.net/note/reject/93088
Search -- https://master.php.net/manage/user-notes.php