imagettfbbox() - gd函数(图像处理)
imagettfbbox()
(PHP 4, PHP 5, PHP 7)
取得使用 TrueType 字体的文本的范围
说明
imagettfbbox(float $size,float $angle,string $fontfile,string $text): array本函数计算并返回一个包围着 TrueType 文本范围的虚拟方框的像素大小。$size像素单位的字体大小。$angle$text将被度量的角度大小。$fontfile TrueType 字体文件的文件名(可以是 URL)。根据 PHP 所使用的 GD 库版本,可能尝试搜索那些不是以'/'开头的文件名并加上'.ttf'的后缀并搜索库定义的字体路径。$text要度量的字符串。imagettfbbox()返回一个含有 8 个单元的数组表示了文本外框的四个角:
0 | 左下角 X 位置 |
1 | 左下角 Y 位置 |
2 | 右下角 X 位置 |
3 | 右下角 Y 位置 |
4 | 右上角 X 位置 |
5 | 右上角 Y 位置 |
6 | 左上角 X 位置 |
7 | 左上角 Y 位置 |
本函数同时需要 GD 库和 FreeType 库。
参见imagettftext()。
I wrote a simple function that calculates the *exact* bounding box (single pixel precision). The function returns an associative array with these keys: left, top: coordinates you will pass to imagettftext width, height: dimension of the image you have to create
As many of you know, this function is bugged in several versions of PHP. It should return the coordinates relative to the baseline of the font. So if your text includes characters like g and p then the bounding box should extend below zero on the Y axis, however it doesn't. This is a problem because imagettftext() positions text using the baseline, so all your text will be misaligned. My solution is to create an image of the desired font and font-size using all ascii characters with imagettfbbox() and imagettftext(). The height of this image is used as the height for the real image. I then analyse the image to get the vertical offset of the text (the background color should be $baseColor) This offset can be used as the baseline for the font (for this font-size). You can use a similar trick for the horizontal offset, but that changes depending on the first character.
Please note that as imageTTFBbox and imageTTFText functions return an array of coordinates which could be negative numbers care must be taken with height and width calculations. The rigth way to do that is to use the abs() function: for an horizontal text: $box = @imageTTFBbox($size,0,$font,$text); $width = abs($box[4] - $box[0]); $height = abs($box[5] - $box[1]); Then to center your text at ($x,$y) position the code should be like that: $x -= $width/2; $y += $heigth/2; imageTTFText($img,$size,0,$x,$y,$color,$font,$text); this because (0,0) page origin is topleft page corner and (0,0) text origin is lower-left readable text corner. Hope this help.
It seems to be worth pointing out that the "points" unit GD2 is using corresponds to 96 dpi, as defined in gd.h: #define GD_RESOLUTION 96 /* pixels per inch */ So if you want to translate the bbox back to font points, you need to multiply all coordinates by 72/96 = 3/4.
Very CLEAR version of func., with example.... [ remember: No spaces before or after the tag, because of header() call, you Roast! ]
This is a function which reformats a text string into a text block of a given width. Usefull when you have a long single line string and want to fit it into a fixed width but don't care about it's height
Here is a function that lets you write a string with your own "font tracking" level (the amount of pixels separating each character). It uses imagettfbbox to determine the width of each character, so it doesn't discriminate against the skinnier of characters. For this example, let $t = the amount of distance in pixels you want to separate each character from its neighbors. Be aware that it currently does not work for angles other than the 0 default (I have no need for that).
Automatic line breaks: This simple function is able automatically create line breaks if you want to write a text on an image. All you have to specify is a maximum length.
If you're looking for easy text alignment, you need to use the imagettfbbox() command. When given the correct parameters, it will return the boundaries of your to-be-made text field in an array, which will allow you to calculate the x and y coordinate that you need to use for centering or aligning your text. A horizontal centering example: $tb would contain: Array ( [0] => 0 // lower left X coordinate [1] => -1 // lower left Y coordinate [2] => 198 // lower right X coordinate [3] => -1 // lower right Y coordinate [4] => 198 // upper right X coordinate [5] => -20 // upper right Y coordinate [6] => 0 // upper left X coordinate [7] => -20 // upper left Y coordinate ) For horizontal alignment, we need to substract the "text box's" width { $tb[2] or $tb[4] } from the image's width and then substract by two. Saying you have a 200px wide image, you could do something like this: This'll give you perfect horizontal center alignment for your text, give or take 1 pixel. Have fun!
Please note that the 3rd argument is really a "path". use instead something like this: or
I have been testing this function for a while now and have come up with many of the same issues that other people have touched upon. Not being able to calculate the width of the text correctly. Or if a solution is found then it won't work with a hanging letter or a negative start letter like 'j'. Like Ralph I also wanted to draw a box around some text and this would require me being pixel perfect with the font. The trouble is I did not know which font would be used or which size. This led me to come up with a solution which I am sharing below. The function above gives the correct x and y coordinates that the text should be drawn from and also gives the actual image width and height. This has been tested with various fonts and sizes ranging from 6 up to 144 points. Some of the output will appear to be incorrect and have an extra pixel on the right, using verdana at size 144 and outputting the character 'Q' for example. This is not an error as this is part of the anti-aliasing of the font output. Example Usage: Return Values: Array ( [0] => -8 [1] => 40 [2] => 715 [3] => 40 [4] => 715 [5] => -177 [6] => -8 [7] => -177 [x] => 6 [width] => 722 [y] => 176 [height] => 216 ) Further notes can be found here along with images of the output of the function http://mikeleigh.com/links/imagettfbbox
measure bg image size, wrap text to fit image width
For mixed text drawing on image, height given by this function is useless and leads to text's hip-hops over the baseline . I will use just this:
I found the bounding boxes a bit tricky to use when trying to rotate AND align text boxes. I wrote this function allows you to set left, right or center aligned text and also rotate it.
Apparently the bounding box returned by imagettftext and imagettfbbox is not the same, and it appears as though imagettftext does a better job at calculating the actual bounding box (maybe because it has to render every character and it then finds out really everywhere it rendered). So, you can create a dummy image render the text to it and get a better box. Here is an example function: If you use this a lot, it would be better to keep one dummy image instead of continually creating and destroying images.
The returned array order is like this: ----------------------------- | X:4 Y:5 | X: Y:7 | ----------------------------- | X:0 Y:1 | X:2 Y:3 | ----------------------------- eg: 4 for the upper left X 5 for the upper left Y and so on.
I found a simple solution that was working for me: Here is the link with a examples: http://www.tuxradar.com/practicalphp/11/2/6#null
It should be noted that the bounding box coordinates returned by this function are inaccurate - bug reports about this have been open for 5 years, so expect this will likely never be fixed. More information here: https://gist.github.com/mindplay-dk/4429153
My solution to below-baseline characters is to simply apply a smaller angle and some padding when calculating your boundaries, so the function thinks your text goes below baseline. For example:
SIMPLE OVERLOADED FUNCTION: Adds TTF Center and Right Alignment to co ordintate points. Correctly demonstrates the use of the bouning box TTF function and the array of coordinates that it returns. After obtaining values and adjusting based on a set of values [L, C, R], it uses simple math based of the length of the text to move it from the origin (x = 0 )
I was viewing the code for calculate the box of a text for a given font but I do not found one that works fine with different angles from zero, so I have made a function simpler than above: With this function you can center an angled string in any image:
I worked out a script that allows the transfer of alphanumeric data to be placed on an image. The HTML feature is img src and the php feature is imagettftext. This simple code will increment from 1 to 3 on images. code:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)