Home > Article > Backend Development > How to set font in php gd
php gd method to set font: 1. Use imagettftext() to draw a custom font; 2. Customize and save the image locally through "imagepng($image,'images/gd_system_font.png');".
The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer
How to set the font in php gd?
GD library painting to change font
1. Use imagettftext() to draw custom fonts, including font angle and font size and font file customization.
2. Customize the image to save locally.
// 创建画布 $image = imagecreatetruecolor(500, 300); // 创建颜色 $white = imagecolorallocate($image, 255, 255, 255); $rand_color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); // 绘制随机颜色 // 绘制一个矩形并使用颜色填充 imagefilledrectangle($image, 0, 0, 500, 300, $white); // 绘画 imagettftext($image, 20, 0, 100, 100, $rand_color, 'fonts/PingFang.ttc', 'Hello World'); // 自定义字体绘制 imagettftext($image,24,mt_rand(0,180),200,200,$rand_color,'fonts/PingFang.ttc','Hi My PHP'); // 自定义字体与角度绘制 // 输出到浏览器 header('content-type:image/png'); imagepng($image); // 保存图像到本地 imagepng($image,'images/gd_system_font.png'); // 销毁画布 imagedestroy($image);
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to set font in php gd. For more information, please follow other related articles on the PHP Chinese website!