Home >Backend Development >PHP Problem >What are the php text drawing functions?

What are the php text drawing functions?

小老鼠
小老鼠Original
2023-08-01 13:24:201281browse

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().

What are the php text drawing functions?

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!

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