【发布时间】:2016-11-16 11:07:57
【问题描述】:
我已经使用 PHP GD 图像库成功地将文本(由 2-3 位数字组成)输出到图像上。接下来,我想定位此文本并将其置于图像中包含的标签下方。我现在通过查看图像和硬编码位置值来使其工作,但这并不理想,因为数字会有所不同,并且每次运行代码时的长度可能都不相同。
这是我目前所拥有的简化版本:
<?php
$font = 'arial.ttf';
$fontsize = 60;
$value1 = $_GET['r'];
$value2 = $_GET['rm'];
$value3 = $_GET['w'];
$image = imagecreatefrompng('image.png');
$fontcolor = imagecolorallocate($image, 255, 255, 255);
$x1 = 80;
$x2 = 160;
$x3 = 280;
$y = 1050;
imagettftext($image, $fontsize, 0, $x1, $y, $fontcolor, $font, $value1);
imagettftext($image, $fontsize, 0, $x2, $y, $fontcolor, $font, $value2);
imagettftext($image, $fontsize, 0, $x3, $y, $fontcolor, $font, $value3);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
我认为我需要使用imagettfbbox,但是如何根据图像中的标签定位框?
是否可以为每个标签设置一个主要位置(因为它们永远不会移动)并根据该位置居中,无论数字的长度如何?例如,如果输入了一个 4 位数字,它将与位于其标签下方的 2 位数字出现在同一位置。
【问题讨论】:
-
php.net/imagettfbbox - 获取框大小,使用计算定位。
标签: php image-processing gd imagettftext