この例は、400x30 ピクセルの白地に Arial
フォントを用いて、黒字 (グレーの影付き) で "Testing..."
と書かれた PNG を作成します。
<?php
// コンテントタイプを設定します
header('Content-Type: image/png');
// 画像を生成します
$im = imagecreatetruecolor(400, 30);
// いくつかの色を生成します
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// 描画する文字列
$text = 'Testing...';
// フォント自身のパスでパスを置き換えます
$font = 'arial.ttf';
// テキストに影を付けます
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// テキストを追加します
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// imagepng() を使用して imagejpeg() よりもクリアなテキストにします
imagepng($im);
imagedestroy($im);
?>