note 16220 deleted from function.imagettfbbox by sniper
| From: | sniper@php.net | Date: | Fri, 07 Nov 2003 12:40:25 +0000 |
| Subject: | note 16220 deleted from function.imagettfbbox by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-60076@lists.php.net to get a copy of this message | ||
Note Submitter: php@remove.this.SPAMBLOCK.arnoldnet.neT
----
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