Home  >  Article  >  Backend Development  >  How to set font in php gd

How to set font in php gd

藏色散人
藏色散人Original
2021-09-17 09:22:483382browse

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');".

How to set font in php gd

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn