php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本 _fei

php GD 和图像处理函数,   用  STHUPO.TTF 字体向图像写入文本

注意:

01)   imagettftext() 这个函数不能使用相对路径, 要想使用相对路径要先使用  putenv() 

02)   STHUPO.TTF  这个字体在当前目录下

 1 // https://php.net/manual/zh/function.imagettftext.php
 2 // https://php.net/manual/zh/function.imagettftext.php
 3 
 4 // !!!为 imagettftext() 这函数使用相对路径做准备----------------------
 5 putenv('GDFONTPATH=' . realpath('.'));
 6 
 7 //1. 绘制图像资源(创建一个画布)
 8 $image = imagecreatetruecolor(500, 300);
 9 
10 //2. 先分配一个绿色
11 $green = imagecolorallocate($image, 22, 153, 0);
12 //3. 使用绿色填充画布
13 // imagefill($image, 0, 0, $green);
14 
15 //使用抗锯齿(antialias)功能  ↓↓↓
16 // imageantialias($image,true);
17 // $red_aa = imagecolorallocate($image, 255, 0, 0);
18 // $red_aa2 = imagecolorallocate($image, 255, 0, 0);
19 // imageline($image, 0, 0, 300, 300, $red_aa);
20 // imageline($image, 100, 0, 400, 300, $red_aa2);
21 //使用抗锯齿(antialias)功能  ↑↑↑
22 
23 //4. 在画布中绘制图像
24 $bai = imagecolorallocate($image, 255, 255, 255);
25 //使用指定的字体文件绘制文字
26 //参数2:字体大小
27 //参数3:字体倾斜的角度
28 //参数4、5:文字的x、y坐标
29 //参数6:文字的颜色
30 //参数7:字体文件
31 //参数8:绘制的文字
32 //------- !!! --------------  imagettftext() 这个函数中的路径使用绝对路径,要想使用相对路径请注意 第一行代码 putenv()
33 imagettftext($image, 30, 30, 200, 250, $bai, './STHUPO.TTF', 'helloworld');
34 // imagettftext($image, 30, 30, 200, 250, $bai, __DIR__.'/STHUPO.TTF', 'helloworld');
35 // imagettftext($image, 30, 30, 200, 250, $bai, realpath('./').'/STHUPO.TTF', 'helloworld');
36 // imagettftext($image, 30, 30, 200, 250, $bai, 'C:/Windows/Fonts/aparajbi.ttf', 'helloworld');
37 // imagettftext($image, 30, 30, 200, 250, $bai, 'C:/Windows/Fonts/STHUPO.TTF', 'helloworld');
38 
39 
40 //5. 在浏览器直接输出图像资源
41 header("Content-Type:image/jpeg");
42 imagejpeg($image);
43 
44 //6. 销毁图像资源
45 imagedestroy($image);

效果图如下: 

原文地址:https://www.cnblogs.com/dafei4/p/11246583.html