【发布时间】:2011-10-24 14:09:33
【问题描述】:
我正在使用 PHP GD 并尝试绘制文本并调整图像大小 刚好适合文字。
我发现了这个问题并尝试了 Martin Geisler 的代码。
Resize image size according to size of text
它看起来工作正常,但是当我尝试其他文本时,有些文本脱离了图像。
当然,我可以添加额外的填充,但所需的填充取决于文本。 这太临时了。
有人知道正确的方法吗?
提前致谢。
// Martin Geisler's version using MS Gothic
header("Content-type: image/png");
$q = 'Image example';
$font = './msgothic.ttc'; // MS Gothic
$size = 30;
$bbox = imageftbbox($size, 0, $font, $q);
$width = $bbox[2] - $bbox[6];
$height = $bbox[3] - $bbox[7];
$im = imagecreatetruecolor($width, $height);
$green = imagecolorallocate($im, 60, 240, 60);
imagefttext($im, $size, 0, -$bbox[6], -$bbox[7], $green, $font, $q);
imagepng($im);
imagedestroy($im);
【问题讨论】:
-
取决于您使用的字体 - 您可能需要使用
imagettfbbox?imageftbbox用于 FreeType 字体。 -
imageftbbox 以相同的结果结束。
-
正如我的问题中所写,我已经阅读了该问题。