imageloadfont()用于加载用户定义的位图.如果您只想使用Arial或任何其他TrueType字体(.ttf)或OpenType字体(.otf)(对于GD lib中的后台支持),那么您需要的是
imagettftext().在使用imagettftext()和编写文本之前为了你的形象,你首先需要知道它是否适合.要知道这一点,您只需要调用
imagettfbbox()并传递字体大小,文本角度(水平文本为0),.ttf或.otf字体文件的路径和文本字符串,它将返回具有8个元素的数组,表示四个点,使文本的边界框(查看详细信息的PHP手册).然后,您可以引用这些数组元素并执行计算,以了解特定文本字符串将占用的宽度和高度.然后,您可以使用这些值创建具有特定宽度和高度的图像,以允许文本整体显示.
这是一个简单的脚本,可以完成你想要做的事,以便让你开始:
/*
* 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手册.