百科狗-知识改变命运!
--

imagefttext() - gd函数(图像处理)

百变鹏仔12个月前 (11-21)阅读数 19#技术干货
文章标签函数

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())返回的图象资源。

$size

The font size to use in points.

$angle

The 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.

$x

The 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.

$y

The y-ordinate. This sets the position of the fonts baseline, not the very bottom of the character.

$color

The index of the desired color for the text, see imagecolorexact().

$fontfile

The path to the TrueType font you wish to use.

imagefttext() - gd函数(图像处理)

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.

$text

Text to be inserted into image.

$extrainfoPossible array indexes for$extrainfo
KeyTypeMeaning
linespacingfloatDefines drawing linespacing

返回值

This function returns an array defining the four points of the box, starting in the lower left and moving counter-clockwise:

0lower left x-coordinate
1lower left y-coordinate
2lower right x-coordinate
3lower right y-coordinate
4upper right x-coordinate
5upper right y-coordinate
6upper left x-coordinate
7upper 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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)