我使用PHP GD並嘗試繪製文本,並使圖像大小適合文本 。使圖像適合文本
我發現這個問題,並嘗試Martin Geisler的代碼。
Resize image size according to size of text
它看上去做工精細,但當我嘗試過其他的文本,一些下了形象。
當然,我可以添加額外的填充,但所需的填充取決於文本。 這太ad-hock了。
有沒有人知道正確的方法來做到這一點?
在此先感謝。
// 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);
http://www.betatechnology.jp/~ao/imageexample.png
根據您所使用的字體 - 你可能需要使用['imagettfbbox'](http://www.php.net /manual/en/function.imagettfbbox.php)? 'imageftbbox'用於FreeType字體。 – dakdad
可能的重複[根據文本大小調整圖像大小](http://stackoverflow.com/questions/1936642/resize-image-size-according-to-size-of-text) – hakre
imageftbbox以相同的結果結束。 – Aoyama