关于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 小时前
构建高效新闻推荐系统:Spring Boot的力量
服务器·spring boot·php
A 八方3 小时前
【漏洞复现】泛微OA E-Office do_excel.php 任意文件写入漏洞
开发语言·安全·php
ac-er88883 小时前
PHP__sleep和__wakeup有什么区别
开发语言·php
什么鬼昵称4 小时前
Pikachu-PHP反序列化
开发语言·javascript·php
DK七七11 小时前
【PHP陪玩系统源码】游戏陪玩系统app,陪玩小程序优势
前端·vue.js·游戏·小程序·php·uniapp
‍。。。12 小时前
Ubuntu 系统崩了,如何把数据拷下来
数据库·ubuntu·php
法迪12 小时前
BugReport中的App Processor wakeup字段意义
开发语言·php·功耗
ac-er888815 小时前
PHP函数 func_num_args() 的作用
java·开发语言·php
xiandong2020 小时前
240925-GAN生成对抗网络
图像处理·人工智能·深度学习·神经网络·生成对抗网络·php·gan
小凡继续努力21 小时前
phpstudy简易使用
php