php用数字5做实心矩形,当文本以数字开头时,php – imagettfbbox会计算错误的矩形...

这个问题是由误解造成的. imagettfbbox的值也定义了你必须从哪里开始绘制 – 通常这些坐标甚至是负的.我总是假设你可以从[0,0]坐标开始.这不是真的,绘图坐标可以是负的.

此功能也在评论中提到并源自PHP.net用户贡献计算开始坐标以及宽度和高度(在相关代码中是正确的).

// Source: http://php.net/manual/en/function.imagettfbbox.php#75407

function imagettfbboxextended($size, $angle, $fontfile, $text) {

/*this function extends imagettfbbox and includes within the returned array

the actual text width and height as well as the x and y coordinates the

text should be drawn from to render correctly. This currently only works

for an angle of zero and corrects the issue of hanging letters e.g. jpqg*/

$bbox = imagettfbbox($size, $angle, $fontfile, $text);

//calculate x baseline

if($bbox[0] >= -1) {

$bbox['x'] = abs($bbox[0] + 1) * -1;

} else {

//$bbox['x'] = 0;

$bbox['x'] = abs($bbox[0] + 2);

}

//calculate actual text width

$bbox['width'] = abs($bbox[2] - $bbox[0]);

if($bbox[0] < -1) {

$bbox['width'] = abs($bbox[2]) + abs($bbox[0]) - 1;

}

//calculate y baseline

$bbox['y'] = abs($bbox[5] + 1);

//calculate actual text height

$bbox['height'] = abs($bbox[7]) - abs($bbox[1]);

if($bbox[3] > 0) {

$bbox['height'] = abs($bbox[7] - $bbox[1]) - 1;

}

return $bbox;

}

但是当绘制时,您必须使用此函数给出的x和y坐标:

imagettftext($im, $fontSize, 0, $bbox["x"], $bbox["y"], $text_color, $font, $text);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值