GDは意外にもやってくれる!
■ソースコード
以下のように、imagettfbbox関数によってテキストボックスのサイズが取得可能である。
<?php $text = 'Open the door!';// string $fontSize = 30; $font = '/usr/share/fonts/bitstream-vera/ACaslonPro-Bold.otf';// path $box = imagettfbbox($fontSize, 0, $font, $text); $img = imagecreatetruecolor($box[2] - $box[6] + 10, $box[3] - $box[7] + 10); $color = imagecolorallocatealpha($img, 210, 0, 0, 255); $backgroundColor = imagecolorallocatealpha($img, 255, 255, 255, 127); imagealphablending($img, true); imagesavealpha($img, true); imagefill($img, 0, 0, $backgroundColor); imagettftext($img, 30, 0, 0, $box[3] - $box[7], $color, $font, $text); header('Content-type: image/png'); imagepng($img); imagedestroy($img); ?>
■画像
こんな感じのができる!