您好!欢迎您的光临!
酒香也怕巷子深酒香也怕巷子深酒香也怕巷子深酒香也怕巷子深
酒香也怕巷子深酒香也怕巷子深酒香也怕巷子深酒香也怕巷子深
酒香也怕巷子深酒香也怕巷子深酒香也怕巷子深酒香也怕巷子深
首页 > 学习心得 > 内容

php函数imagettftext使用注意事项

学习心得 2023-02-27 21:10:14 0

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

函数的使用

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字体的路径。

例子

  1. $im = imagecreatetruecolor(1080, 1920);
  2. $color = imagecolorallocate($im, 255, 255, 255); //填充画布背景色
  3. imagefill($im, 0, 0, $color);
  4. $font_color = ImageColorAllocate ($im, 28, 28, 28); //字体颜色
  5. $font_path = Yii::getAlias('@api').'/web/font/PingFang.ttc'; //字体文件
  6. imagettftext ( $im, 50, 0, 500, 400, $font_color, $font_path, 'PHP学习网' );
  7. ob_start(); //启用输出缓冲
  8. header('Content-type:image/png');
  9. imagepng($im); //输出图像

碰到的问题

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

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

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

文章来源于网络,版权归原作者所有。如有问题请联系本站