imagettftext

(PHP 3, PHP 4, PHP 5)

imagettftext -- 用 TrueType 字体向图像写入文本

说明

array imagettftext ( resource image, int size, int angle, int x, int y, int color, string fontfile, string text )

imagettftext() 将字符串 text 画到 image 所代表的图像上,从坐标 xy(左上角为 0, 0)开始,角度为 angle,颜色为 color,使用 fontfile 所指定的 TrueType 字体文件。根据 PHP 所使用的 GD 库的不同,如果 fontfile 没有以 '/'开头,则 '.ttf' 将被加到文件名之后并且会搜索库定义字体路径。

xy 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的右上角。

angle 以角度表示,0 度为从左向右阅读文本(3 点钟方向),更高的值表示逆时针方向(即如果值为 90 则表示从下向上阅读文本)。

fontfile 是想要使用的 TrueType 字体的文件名。

text 是文本字符串,可以包含 UTF-8 字符序列(形式为:{)来访问字体中超过前 255 个的字符。

color 是颜色的索引值。使用某颜色索引值的负值具有关闭防混色的效果。

imagettftext() 返回一个含有 8 个单元的数组表示了文本外框的四个角,顺序为坐下角,右下角,右上角,左上角。这些点是相对于文本的而和角度无关,因此“左上角”指的是以水平方向看文字时其左上角。

本例中的脚本将生成一个黑色的 400x30 的 JPEG 图像,其中有白色 Arial 字体写的 "Testing..."。

例子 1. imagettftext() 例子

<?php
  header
("Content-type: image/jpeg");
  
$im = imagecreate(400,30);
  
$white = imagecolorallocate($im, 255,255,255);
  
$black = imagecolorallocate($im, 0,0,0);

  
// Replace path by your own font path
  
imagettftext($im, 20, 0, 10, 20, $black, "/path/arial.ttf",
  
"Testing... Omega: &amp;#937;");
  
imagejpeg($im);
  
imagedestroy($im);
?>

本函数同时需要 GD 库和 FreeType 库。

参见 imagettfbbox()