新增到GD库.动态文本图像调整大小


New To The GD library. Dynamic text on image resizing

开门见山。我正在做一个新项目,我以前从来没有机会和GD一起工作。我已经使用PHP大约3年了,所以我对它并不陌生,只是直到现在才有理由探索这些特性。

所以我的代码是:
<?php
header("Content-type: image/png");
$string = $_POST['userinput'];;
$im = imagecreatefrompng("images/tshirt.png");
$x = 175;
$y = 240;
$font = 'arial.ttf';
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);   //white background
$textColor = imagecolorallocate ($im, 0, 0, 0);   //black text
imagettftext($im, 32, 0, $x, $y, $textColor, $font, $string);
imagepng ($im); 
imagedestroy($im);

?>

现在我知道$x和$ y是控制坐标的东西,但是我需要代码能够根据某人输入的内容调整大小和居中。这上面最多有15个字符。该应用程序是一个定制的t恤网站。谢谢你的帮助。

我已经尝试了一些方法,但都无济于事,所以我希望有人能告诉我一些我没有看到的东西。

谢谢,贾斯汀

您可以使用imagettfbox()来计算结果文本的大小。这将允许您进行居中计算,从而为使用imagettftext()绘制的实际文本生成坐标。