Home > Backend Development > PHP Problem > How to set font in php gd

How to set font in php gd

藏色散人
Release: 2023-03-12 16:12:01
Original
3478 people have browsed it

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template