imageloadfont()
は、ユーザー定義のビットマップをロードするために使用されます。 Arialやその他のTrueTypeフォント(.ttf)やOpenTypeフォント(.otf)(GD libの後者のサポートはバグ)を使用したい場合は、imagettftext()
が必要です。 imagettftext()
を使用してイメージにテキストを書き込む前に、まずそれが適合するかどうかを知る必要があります。これを知るには、imagettfbbox()
に電話をかけて、フォントサイズ、テキストの角度(水平のテキストの場合は0)、.ttfまたは.otfフォントファイルのパス、テキストの文字列などを渡す必要があります。配列の8つの要素が4つの点を表し、テキストの境界ボックスを作成します(詳細はPHPマニュアルを参照してください)。それらの配列要素を参照し、その特定の文字列が占有する幅と高さを知るために計算を実行することができます。これらの値を使用して、特定の幅と高さの画像を作成し、テキスト全体を表示することができます。
<?php # Script 1
/*
* This page creates a simple image.
* The image makes use of a TrueType font.
*/
// Establish image factors:
$text = 'Sample text';
$font_size = 12; // Font size is in pixels.
$font_file = 'Arial.ttf'; // This is the path to your font file.
// Retrieve bounding box:
$type_space = imagettfbbox($font_size, 0, $font_file, $text);
// Determine image width and height, 10 pixels are added for 5 pixels padding:
$image_width = abs($type_space[4] - $type_space[0]) + 10;
$image_height = abs($type_space[5] - $type_space[1]) + 10;
// Create image:
$image = imagecreatetruecolor($image_width, $image_height);
// Allocate text and background colors (RGB format):
$text_color = imagecolorallocate($image, 255, 255, 255);
$bg_color = imagecolorallocate($image, 0, 0, 0);
// Fill image:
imagefill($image, 0, 0, $bg_color);
// Fix starting x and y coordinates for the text:
$x = 5; // Padding of 5 pixels.
$y = $image_height - 5; // So that the text is vertically centered.
// Add TrueType text to image:
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_file, $text);
// Generate and send image to browser:
header('Content-type: image/png');
imagepng($image);
// Destroy image in memory to free-up resources:
imagedestroy($image);
?>
変更値に応じて、ニーズに合わせて:ここで
はあなたが始めるためにやろうとしているものを実現する簡単なスクリプトです。 PHPのマニュアルを必ずお読みください。 GD2、imagettfbboxのフォントサイズで
を取ります[imagettfbbox](http://www.php.net/manual/en/function.imagettfbbox.php)と[imagettftext](http://php.net/manual/en/function.imagettftext.php)を参照してください。 – Vatev