imagettfbbox与imagettftext函数的坐标系统认识
——————————————————————————–
imagettfbbox函数的坐标系统的认识
写 ttf 文字到图中。
语法: array imagettfbbox(int size, int angle, string fontfile, string text);
返回值: 数组
函数种类: 图形处理
内容说明
本函数将 ttf (truetype fonts) 字型文字写入图片。参数 size 为字形的尺寸;angle 为字型的角度,顺时针计算,0 度为水平,也就是三点钟的方向 (由左到右),90 度则为由下到上的文字;x,y 二参数为文字的坐标值 (原点为左下角);参数 col 为字的颜色;fontfile 为字型文件名称,亦可是远端的文件;text 当然就是字符串内容了。返回值为数组,包括了八个元素,头二个分别为左下的 x、y 坐标,第三、四个为右下角的 x、y 坐标,第五、六及七、八二组分别为右上及左上的 x、y 坐标。治募注意的是欲使用本函数,系统要装妥 gd 及 freetype 二个函数库。
使用范例
——————————————————————————–
//header(“content-type:image/png”);
//$im = imagecreate(400,30);
//$black = imagecolorallocate($im, 0,0,0);
//$white = imagecolorallocate($im, 255,255,255);
$fontarea = imagettfbbox(20,0,”font/arial.ttf”,”i am number one !!”);//返回值赋给$fontsize
//$fontindex=sizeof($fontsize);//获得数组大小
//$string=null;
while ( list( $key, $val = each( $fontarea
{
echo “$key => $val
“;
}
//imagettftext($im, 20, 0, 10, 24, $white, “font/arial.ttf”, “i am number one !!”);
//imagepng($im);
//imagedestroy($im);
//计算动态图象的高和宽
$font_width = $fontarea[2]-$fontarea[0];//256
$font_height = $fontarea[1]-$fontarea[7];//19
$images_width = $font_width+10;//定义生成计数器图象宽度
//$images_height=15;//定义生成计数器图象高度
$images_height = $font_height+10;
$font_location_x =5;
$font_location_y = ($images_height-$font_height)/2+$fontarea[7];
echo “images_width=$images_width
images_height=$images_height
“;
echo “font_width=$font_width
font_height=$font_height
“;
echo “font_location_x=$font_location_x
font_location_y=$font_location_y
“;
?>
——————————————————————————–
使用范例的返回内容:
0 => 2
1 => -1
2 => 258
3 => -1
4 => 258
5 => -20
6 => 2
7 => -20
images_width=266
images_height=29
font_width=256
font_height=19
font_location_x=5
font_location_y=-15
——————————————————————————–
使用图像分析:
[img:d6855fa6bf]http://www.freewebs.com/macleo/images/php001.png[/img:d6855fa6bf]
自己的分析:
如果,我们按照《php 4完全中文手册》的关于imagettftext函数和imagettfbbox函数的第一组返回坐标的定义来讲,它们都是左下角的坐标。
那么,我觉得php中的imagettftext函数的字体的原点坐标就是在左上角了! 因为,我使用imagettftext函数返回的第一组坐标是(13,24),而用imagettfbbox函数返回的第一组坐标是(2,-1),所以,从这两个坐标数值上,我们就可以判断出这两个函数要么有不同的坐标系统(imagettftext函数的原点坐标在左上角,而imagettfbbox函数的原点坐标在左下脚),要么就是《php 4完全中文手册》的说法是错误的!为了让我们使用php中的gd库调用的ttf字体能按照,我们的意图显示在一个动态生成的图象上,我们要计算出imagettftext函数的字体的大小和图象的大小,而最终确定我们的字体的显示位置。