关于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); 
相关推荐
yangSnowy4 小时前
webman框架虚拟数据填充fakerphp/faker插件的使用
php
简鹿视频5 小时前
视频转mp4格式具体作步骤
ffmpeg·php·音视频·实时音视频
liebe1*15 小时前
第十一章 密码学
服务器·密码学·php
一分半心动8 小时前
lnmp架构 mysql数据库Cannot assign requested address报错解决
linux·mysql·php
catchadmin10 小时前
PHP 开发者指南 如何在 Composer 中使用本地包
开发语言·php·composer
刘孬孬沉迷学习12 小时前
GTP协议
开发语言·学习·5g·php·信息与通信
C+++Python12 小时前
PHP 反射 API
android·java·php
bleach-12 小时前
buuctf系列解题思路祥讲--[网鼎杯 2020 青龙组]AreUSerialz1——文件包含漏洞,PHP代码审计,php伪协议,php反序列化
开发语言·安全·web安全·网络安全·渗透测试·php
zhaotiannuo_199812 小时前
渗透测试之后端编程PHP
开发语言·chrome·php
翼龙云_cloud13 小时前
腾讯云云渠道商:如何利用镜像实现跨云平台迁移?
运维·服务器·云计算·php·腾讯云