note 16220 added to function.imagettfbbox
| From: | php at remove dot this dot SPAMBLOCK dot arnoldnet dot neT | Date: | Sun, 21 Oct 2001 01:58:08 +0000 |
| Subject: | note 16220 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-19217@lists.php.net to get a copy of this message | ||
I don't understand why all the commenters above seem to be having so much trouble with this
truetype bounding box thing. Here's my solution. The color code was borrowed from others, and
things get a little more complicated if you want to overlay on another image, but here's
probably everything you'll need for just getting a proper label (with baselines, angles, and
other whatnot) from a bit of text, assuming I copied and pasted right:
<?php
// local setting
$fontbase = "whatever/fonts/";
// image.php
// by Kenneth Arnold <php@remove.this.SPAMBLOCK.arnoldnet.neT>
// written because none of the other examples on the PHP manual page work absolutely
// right all the time. Based on some of those examples that didn't work.
//vars
$fontfile = "$fontbase/timesnr.ttf"; // you may want to have this user-definable...
if (!$angle) $angle=0;
$size = ImageTTFBBox ($fontsize, $angle, $fontfile, $text);
$dx = max($size[2], $size[4]) - min($size[0], $size[6]); // extreme x values
$dy = max($size[1], $size[3]) - min($size[5], $size[7]); // extreme y values
$im = imagecreate ($dx, $dy);
$bkgc = ImageColorAllocate ($im, 255, 255, 255);
$txtc = ImageColorAllocate ($im, 197, 196, 230);
ImageTTFText ($im, $fontsize, $angle, 0, -min($size[5], $size[7]), $txtc, $fontfile, $text);
Header ("Content-type: image/png");
ImagePng ($im);
ImageDestroy ($im);
?>
Call that with
"/whateveryoucalledit.php?text=sometext&fontsize=somefontsize&angle=10" (angle
optional).
You can extend that, using dx and dy, to center that text in a larger image. Note that this
doesn't try to fix the antialiasing and colors "issue"; see the comments for
ImageTTFText for some solutions to that.
I welcome questions and comments at the email above, appropriately sanitized.
Just an inexperienced PHP hacker trying to help,
Ken
--
http://www.php.net/manual/en/function.imagettfbbox.php
http://master.php.net/manage/user-notes.php?action=edit+16220
http://master.php.net/manage/user-notes.php?action=delete+16220
http://master.php.net/manage/user-notes.php?action=reject+16220