四时宝库

程序员的知识宝库

php函数imagettftext使用注意事项

我们在做生成分享图,生成验证码时,避免不了要使用此函数,此函数的作用是在图片上生成对应的字体。

函数的使用

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

image – 图像资源。
size – 字体大小。
angle – 以度为单位的角度,0度为从左到右读取文本。
x – x和y给出的坐标将定义第一个字符的基点。
y – 纵坐标。
color – 颜色索引。
fontfile – TrueType字体的路径。

例子

$im = imagecreatetruecolor(1080, 1920);

$color = imagecolorallocate($im, 255, 255, 255); //填充画布背景色

imagefill($im, 0, 0, $color);

$font_color = ImageColorAllocate ($im, 28, 28, 28); //字体颜色

$font_path = Yii::getAlias('@api').'/web/font/PingFang.ttc'; //字体文件

imagettftext ( $im, 50, 0, 500, 400, $font_color, $font_path, 'PHP学习网' );

ob_start(); //启用输出缓冲

header('Content-type:image/png');

imagepng($im); //输出图像

碰到的问题

1、字体路径问题:为减少不必要的麻烦,请一定要使用绝对路径

2、字体格式:建议使用ttf格式,ttc有可能在linux上不兼容。

3、安装GD库扩展,同时需要freetype扩展,并且是高版本,请安装2.3.11以上版本。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接