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

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

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

imageftbbox()

(PHP 4 >= 4.0.7, PHP 5, PHP 7)

给出一个使用 FreeType 2 字体的文本框

说明

imageftbbox(float $size,float $angle,string $fontfile,string $text[,array $extrainfo]): array

This function calculates and returns the bounding box in pixels for a FreeType text.

参数

$size

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

字体的尺寸。根据 GD 的版本,为像素尺寸(GD1)或点(磅)尺寸(GD2)。

$angle

Angle in degrees in which$textwill be measured.

$fontfile

The name of the TrueType font file(can be a URL). Depending on which version of the GD library that PHP is using, it may attempt to search for files that do not begin with a leading '/' by appending '.ttf' to the filename and searching along a library-defined font path.

$text

The string to be measured.

$extrainfoPossible array indexes for$extrainfo
KeyTypeMeaning
linespacingfloatDefines drawing linespacing

返回值

imageftbbox() returns an array with 8 elements representing four points making the bounding box of the text:

0lower left corner, X position
1lower left corner, Y position
2lower right corner, X position
3lower right corner, Y position
4upper right corner, X position
5upper right corner, Y position
6upper left corner, X position
7upper left corner, Y position

The points are relative to thetextregardless of the$angle, so "upper left" means in the top left-hand corner seeing the text horizontally.

范例

imageftbbox() example

注释

Note:此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。

Note:此函数仅在PHP 编译时加入 freetype 支持时有效(--with-freetype-dir=DIR)。

更新日志

版本说明
4.3.5$extrainfowas made optional.
imagettfbbox() returns an array with 8 elements representing four points making the bounding box of the text: 
0 lower left corner, X position 
1 lower left corner, Y position 
2 lower right corner, X position 
3 lower right corner, Y position 
4 upper right corner, X position 
5 upper right corner, Y position 
6 upper left corner, X position 
7 upper left corner, Y position 
The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner seeing the text horizontally.
Here is a handy example I used to center "dynamic text" onto an image. 
Ex. Say you want to center a clients IP Address onto a picture. 
$ip=$_SERVER['REMOTE_ADDR'];  
$details = imageftbbox($fontsize, 0, $font, $ip, array("linespacing" => 1));
$xcoord = ($imgwidth - $details[4]) / 2; // this will return the x coordinate centered to your specific image. Make sure you set $imgwidth to the width of the image you are using.   
imagettftext($image, $fontsize, 0, $xcoord, $ycoord, $fontcolor, $font, $ip);
This function can be used to generate right-aligned text. Just work out how wide the text image is and position it accordingly. Example:
$i_width = 200;
$i_height = 40;
$string = "Hello World!";
$pointsize = 10;
$fontfile = "/usr/local/lib/ttf/Helve.ttf";
$im = imagecreate($i_width, $i_height);
$black = imagecolorallocate ($im, 0, 0, 0);
$white = imagecolorallocate ($im, 255, 255, 255);
$string_size = ImageFtBbox($pointsize, 0, $fontfile, $string, array("linespacing" => 1));
$s_width = $string_size[4];
$s_height = $string_size[5];
ImageFtText($im, $pointsize, 0, $i_width - $s_width - 1, 0 - $s_height, $white, $fontfile, $string, array("linespacing" => 1));
Header ("Content-type: image/png");
ImagePNG ($im);
ImageDestroy ($im);
//EXAMPLE - Center text
function newText($im, $size, $angle= 0, $x, $y, $color, $font, $text,$align = "left",$border=false,$width=0,$height=0){
  
  if($align == "center")
  {
    if ($border == true ){
      imagerectangle($im, $x, $y, $x +$width, $y + $height, $color);
    }
    $bbox = imageftbbox($size, 0, $font, $text);
    // Marcamos el ancho y alto
    $s_width = $bbox[4];
    $s_height = $bbox[5]; 
    
    $y = $y + ($height-$s_height)/2;
    $x = $x + ($width-$s_width)/2;
  }
   
  imagettftext($im, $size, $angle, $x, $y, $color, $font, $text);
}
ah... the problem between imageftbbox() and imagefttext() lies in the mirroring of the y-axes.
Below you see, for a font-size 16 the boudingboxes of "b", "p" and "bp":
i've found a work around for this situation
it seems that height is directly proportional to line spacing so you just have to apply the same factor to image height
for example :
$spacing = 0.7;
$params = array("linespacing" => $spacing);
$box = imageftbbox ($size, 0, $font, $text, $params);
$tw=$box[4]-$box[0]; //image width
$th=($box[1]-$box[5])*$spacing; //image height
ImageFTBBox returns a bounding box, not metrics, as some (most?) of the notes above seem to assume. The 8 values it returns specify the 4 corners of this bounding box. So to properly determine the width and height of a string you need to do:
$bbox = ImageFTBBox(...);
$width = abs($bbox[0]) + abs($bbox[2]); // distance from left to right
$height = abs($bbox[1]) + abs($bbox[5]); // distance from top to bottom
For alignment i used this method:
if($align == "center" || $align == "right")
  {
    $verticaltxtspace = $backwidth - (2 * $posx);    
    $spacepositions = imagettfbbox($size, $angle, "fonts/verdanaz.ttf", " ");     
    $spacepx = $spacepositions[4] - $spacepositions[0];    
    
    // Split text in lines
    $lines = split("[\r][\n]", $text);    
    for($count = 0; $count 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

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

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

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