关于PHP中GD库函数imagettfbbox的坐标

使用GD库处理图片的时候需要紧跟在某串字符串之后写出另一个格式不同的字符串,所以需要使用imagettfbox函数来获取第一个字符串的宽度,研究了一下发现这个函数的坐标很感人,所以写下记录。虽然复杂但是估计几张图估计就看懂了。

添加图片注释,不超过 140 字(可选)

文件宽高200像素*200像素 红色格子为画的参考线间距10px,浅蓝色线为使用imagettftext写入字符串时候传入的参考位置,相关函数:

php 复制代码
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

写入位置X,Y都为40,写入文本是"¥120",使用微软雅黑字体。

文字外部的蓝色框是根据imagettfbox函数返回的坐标画的外框,函数解释:

php 复制代码
array imagettfbbox ( float $size , float $angle , string $fontfile , string $text )
imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:
0 	左下角 X 位置
1 	左下角 Y 位置
2 	右下角 X 位置
3 	右下角 Y 位置
4 	右上角 X 位置
5 	右上角 Y 位置
6 	左上角 X 位置
7 	左上角 Y 位置

上图输出坐标:

scss 复制代码
array(8) {
  [0]=>
  int(6)
  [1]=>
  int(0)
  [2]=>
  int(74)
  [3]=>
  int(0)
  [4]=>
  int(74)
  [5]=>
  int(-20)
  [6]=>
  int(6)
  [7]=>
  int(-20)
}

附上对比图:

添加图片注释,不超过 140 字(可选)

使用的php文件代码贴在最后,修改自【PHP】GD库笔记 初探GD库的坐标

ini 复制代码
<?php  
$width = 200;  
$height = 200;  
$canvas_w = $height + 1;  
$canvas_h = $height + 1;  
$im = imagecreatetruecolor($canvas_w,$canvas_h);  
  
$white = imagecolorallocate($im, 255, 255, 255);  
$red = imagecolorallocate($im, 255, 0, 0);  
$green = imagecolorallocate($im, 0, 255, 0);  
$blue = imagecolorallocate($im, 0, 0, 255);  
$mixed = imagecolorallocate($im, 0, 255, 255);  
$line_color = $blue;  
imagefill($im, 0, 0, $white);  
$fontsize = 20;  
$fontfile = "msyh.ttf";  
$text = "¥120";  
$font_x = 40;  
$font_y = 40;  
  
for($start_x = 0;$start_x <= $canvas_w;$start_x += 10){  
    if($start_x == $font_x){  
        imageline($im,$start_x,0,$start_x,$width,$mixed);  
    }else{  
        imageline($im,$start_x,0,$start_x,$width,$red);  
    }  
}  
  
for($start_y = 0;$start_y <= $canvas_h;$start_y += 10){  
    if($start_y == $font_y){  
        imageline($im,0,$start_y,$height,$start_y,$mixed);  
    }else{  
        imageline($im,0,$start_y,$height,$start_y,$red);  
    }  
}  
  
imagettftext($im ,$fontsize,0,$font_x,$font_y,$green ,$fontfile ,$text);  
$box = imagettfbbox($fontsize,0,$fontfile,$text);  
var_dump($box);
$left_bottom = array($box[0],$box[1]);  
$right_bottom = array($box[2],$box[3]);  
$right_top = array($box[4],$box[5]);  
$left_top = array($box[6],$box[7]);  
  
$box_width = max(abs($box[2] - $box[0]),abs($box[4] - $box[6]));  
$box_height = max(abs($box[7] - $box[1]),abs($box[5] - $box[3]));  
  
$abs_y1 = abs($box[1]);  
$abs_y2 = abs($box_height - $abs_y1);  
$left_bottom_x  = $font_x +$box[0];  
$left_bottom_y  = $font_y +$box[1];  
$right_bottom_x = $left_bottom_x + $box_width;  
$right_bottom_y = $left_bottom_y;  
$right_top_x    = $right_bottom_x;  
$right_top_y    = $right_bottom_y-$box_height;  
$left_top_x     = $left_bottom_x;  
$left_top_y     = $right_top_y;  
  
imageline($im,$left_bottom_x,$left_bottom_y,$right_bottom_x,$right_bottom_y,$blue);  
imageline($im,$right_bottom_x,$right_bottom_y,$right_top_x,$right_top_y,$blue);  
imageline($im,$right_top_x,$right_top_y,$left_top_x,$left_top_y,$blue);  
imageline($im,$left_top_x,$left_top_y,$left_bottom_x,$left_bottom_y,$blue);  
  
imagejpeg($im,'res.jpg',100);  
imagedestroy($im); 
相关推荐
ThatITs3 小时前
【无标题】
php
Java后端的Ai之路3 小时前
【Python 教程14】- 网络编程
网络·python·php
暴走十八步3 小时前
PHP+vscode开启调试debug
开发语言·vscode·php
catchadmin3 小时前
Laravel AI SDK 正式发布
人工智能·php·laravel
learning-striving3 小时前
kali连不上网解决方法
linux·开发语言·网络·php·kali
darkb1rd13 小时前
四、PHP文件包含漏洞深度解析
网络·安全·php
毕设源码-邱学长16 小时前
【开题答辩全过程】以 基于PHP的发热病人管理平台的设计与实现为例,包含答辩的问题和答案
开发语言·php
catchadmin18 小时前
2026 年 PHP 前后端分离后台管理系统推荐 企业级方案
开发语言·php
小李独爱秋18 小时前
计算机网络经典问题透视:蜂窝网络切换如何“扼杀”你的TCP连接?
网络·网络协议·tcp/ip·计算机网络·php·信息与通信
Data_Journal18 小时前
如何使用 Python 解析 JSON 数据
大数据·开发语言·前端·数据库·人工智能·php