imagefttext() - gd函数(图像处理)
imagefttext()
(PHP 4 >= 4.0.7, PHP 5, PHP 7)
使用 FreeType 2 字体将文本写入图像
说明
imagefttext (resource $image,float $size,float $angle,int $x,int $y,int $color,string $fontfile,string $text[,array $extrainfo] ) : array参数
$image由图象创建函数(例如imagecreatetruecolor())返回的图象资源。
$sizeThe font size to use in points.
$angleThe angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text.
$xThe coordinates given by$xand$ywill define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the imagestring(), where$xand$ydefine the upper-left corner of the first character. For example, "top left" is 0, 0.
$yThe y-ordinate. This sets the position of the fonts baseline, not the very bottom of the character.
$colorThe index of the desired color for the text, see imagecolorexact().
$fontfileThe path to the TrueType font you wish to use.
Depending on which version of the GD library PHP is using,when$fontfiledoes not begin with a leading/then.ttfwill be appendedto the filename and the library will attempt to search for that filename along a library-defined font path.
When using versions of the GD library lower than 2.0.18, aspacecharacter, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message:Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.
In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.
$textText to be inserted into image.
$extrainfoKey | Type | Meaning |
---|---|---|
linespacing | float | Defines drawing linespacing |
返回值
This function returns an array defining the four points of the box, starting in the lower left and moving counter-clockwise:
0 | lower left x-coordinate |
1 | lower left y-coordinate |
2 | lower right x-coordinate |
3 | lower right y-coordinate |
4 | upper right x-coordinate |
5 | upper right y-coordinate |
6 | upper left x-coordinate |
7 | upper left y-coordinate |
范例
imagefttext() example
注释
Note: 此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。
Note: 此函数仅在PHP 编译时加入 freetype 支持时有效(--with-freetype-dir=DIR)。
更新日志
版本 | 说明 |
---|---|
4.3.5 | $extrainfowas made optional. |
After spending the evening with some work on automatically generated images, I had the idea to switch of anti-aliasing (looking, if some font would look better that way), which turned out not to be quite so easy. Actually you have to use the negative of the desired color to switch of antialising. I include the corresponding line from my code (line split up): // USE NEGATIVE OF DESIRED COLOR TO SWITCH OF ANTI-ALIASING ImageFTText ($neuesBild,$fontsize,$fontangle,$TextPosX,$TextPosY, -$custom_fg,$fonttype,$text,array());
When compiling PHP with FreeType 2 support, you'll probably have some problems if - for example - you use debian and didn't compile freetype2 yourself... If configure fails after saying "If configure fails, try --with-xpm-dir..." you most likely have FreeType1 installed, but not freetype2 ... Do this as root : apt-get install libfreetype6-dev It took me some time to find out that apt-get install freetype2 is actually installing freetype1 ...
If you're interested in turning off FreeType hinting, search for the following line in the gd source (gdft.c): err = FT_Load_Glyph (face, glyph_index, FT_LOAD_DEFAULT); and replace it with err = FT_Load_Glyph (face, glyph_index, FT_LOAD_NO_HINTING); Recompile GD, and vola: beauteous antialiasing.
I'm not sure if this is a PHP issue or an GD issue, but after upgrading to PHP 5.3.2, text written at an angle has become top-justified (so "N" and "n" have the same top, but the bottom of the "N" is lower than the bottom of the "n". I've written a kludgy work-around, which writes the text to a non-rotated temporary image, then copies the temporary image, rotated onto the main image. The kludginess is to get around the fact that I can't seem to extract the font info, particularly the distance between the baseline and the very bottom (I've hard-coded it as 30% of the font size) I hope the bug can be fixed (if it is indeed a bug) or that others can improve this code: -Dave
This function is very simular to imageffttext(), you may find the information provided on its manual page helpful: http://php.net/imagettftext
For a design project I am required to have spacing between characters; since imagefttext does not support this feature I have created a function which does support this. The arguments are identical to imagefttext, accept that (array)$extrainfo now accepts the 'character_spacing' spacing parameter. The return values are as expected, and include the image boundaries of the entire string including the character spacing. The downside is that $angle rotates each letter instead of rotating the entire word (could be seen as a feature on its own). I hope this is of some use to someone. - KeepSake
realpath(".") realpath(getenv("SCRIPT_FILENAME")); could be different. This helped when setting GDFONTPATH.
I had trouble working out how to accurately represent fonts in point sizes when constructing charts that had a user-customisable output DPI (basically, the user could specify the size of the chart in mm - or any other physical measure - and the DPI to create arbitrarily-sized charts to work properly in real printed documents). GD1 was OK as it used pixels for font rendering, but GD2 uses points, which only makes any sense if you know the DPI that it assumes when rendering text on the image surface. I have not been able to find this anywhere in this documentation but have examined the GD2 source code and it appears to assume a DPI of 96 internally. However, this can easily be customised in the GD2 source so it cannot be assumed that all PHP interpreters out there have a GD2 compiled using 96dpi internally. If it does, and you are using it to construct images whose target DPI is not 96, you can calculate the point size to supply to imageftbox() and imagefttext() like this:
I am using php 5.1.2 on a winxp machine. I was getting into the TrueType fonts and wanted to see which ones would look best incorporated into web images. So I created the following script that prints out samples of all the TrueType fonts found in my C:\Windows\Fonts directory. The script takes only one request parameter - 'fsize'. It stands for font-size and lets you see each font in any size you wish -- I limited it to values between 5 and 48. Hope this helps someone other than me :) I apologize in advance if any of my code is not the prettiest-written php code even seen -- I have only been coding in php for the past week (I'm a perl-guy usually).
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!