关于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); 
相关推荐
小红帽2.01 小时前
开源PHP在线客服系统源码搭建教程
开发语言·开源·php
CRMEB定制开发2 小时前
CRMEB 框架中 PHP 上传扩展开发:涵盖本地上传及阿里云 OSS、腾讯云 COS、七牛云
阿里云·php·腾讯云·微信商城·php商城源码
hao_wujing3 小时前
攻击模型的恶意行为检测
网络·数据库·php
Yungoal18 小时前
php & apache构建 Web 服务器
服务器·php·apache
浩浩测试一下19 小时前
Authpf(OpenBSD)认证防火墙到ssh连接到SSH端口转发技术栈 与渗透网络安全的关联 (RED Team Technique )
网络·网络协议·tcp/ip·安全·网络安全·php
孤寂大仙v20 小时前
【计算机网络】网络层IP协议与子网划分详解:从主机通信到网络设计的底层逻辑
tcp/ip·计算机网络·php
vortex51 天前
浅谈 Linux 防火墙:从原理到实践
linux·网络·php
come112341 天前
Claude 写 PHP 项目的完整小白教程
开发语言·php
Bruce_Liuxiaowei1 天前
PHP文件读取漏洞全面剖析:触发点与利用技术
开发语言·php
hopetomorrow1 天前
学习路之PHP--webman安装及使用
android·学习·php