PHP commonly used drawing text functions are: 1. "imagestring()" function, which can draw simple strings on images; 2. "imagettftext()" function, which can draw TrueType fonts on images 3. The "imagefttext()" function can also draw a TrueType font string on the image. The usage is slightly different from imagettftext().
The operating environment of this tutorial: Windows 10 system, PHP8.1.3 version, Dell G3 computer.
PHP is a widely used programming language, especially suitable for web development. When developing web pages, you often need to use the function of drawing text. PHP provides some functions that can be used to draw text. Some of the commonly used functions are introduced below.
1. imagestring(): This function can draw a simple string on the image. Its syntax is as follows:
bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color ).
Parameter description:
- image: Image resource.
- font: font number, which can be one of 1, 2, 3, 4, and 5. Where 1 represents a 5x5 font, 2 represents a 7x7 font, and so on.
- x: x coordinate of the beginning of the string.
- y: The y coordinate of the beginning of the string.
- string: The string to draw.
- color: The color of the string.
Sample code:
``` // 创建一个画布 $image = imagecreate(200, 200); // 设置颜色 $color = imagecolorallocate($image, 255, 0, 0); // 绘制字符串 imagestring($image, 5, 50, 50, "Hello, PHP!", $color); // 输出图像 header("Content-type: image/jpeg"); imagejpeg($image); imagedestroy($image); ```
2. imagettftext(): This function can draw a string using TrueType font on the image. Its syntax is as follows:
bool imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
Parameter description:
- image: Image resource.
- size: font size.
- angle: string tilt angle.
- x: x coordinate of the beginning of the string.
- y: The y coordinate of the beginning of the string.
- color: The color of the string.
- fontfile: The path to the TrueType font file.
- text: The string to draw.
Sample code:
``` // 创建一个画布 $image = imagecreate(200, 200); // 设置颜色 $color = imagecolorallocate($image, 255, 0, 0); // 设置字体文件路径 $fontfile = 'arial.ttf'; // 绘制字符串 imagettftext($image, 20, 0, 50, 50, $color, $fontfile, "Hello, PHP!"); // 输出图像 header("Content-type: image/jpeg"); imagejpeg($image); imagedestroy($image); ```
3. imagefttext(): This function can also draw TrueType font strings on the image. The usage is slightly different than imagettftext(). Its syntax is as follows:
bool imagefttext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text [, array $extrainfo ] )
Parameter description:
- image: Image resource.
- size: font size.
- angle: string tilt angle.
- x: x coordinate of the beginning of the string.
- y: The y coordinate of the beginning of the string.
- color: The color of the string.
- fontfile: The path to the TrueType font file.
- text: The string to draw.
- extrainfo: Some additional drawing parameters, such as shadow effects.
Sample code:
``` // 创建一个画布 $image = imagecreate(200, 200); // 设置颜色 $color = imagecolorallocate($image, 255, 0, 0); // 设置字体文件路径 $fontfile = 'arial.ttf'; // 绘制字符串 imagefttext($image, 20, 0, 50, 50, $color, $fontfile, "Hello, PHP!"); // 输出图像 header("Content-type: image/jpeg"); imagejpeg($image); imagedestroy($image); ```
The above are several commonly used text drawing functions in PHP. Through these functions, we can draw various styles of strings in images, adding rich visual effects to web page development.
The above is the detailed content of What are the php text drawing functions?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
