Home  >  Article  >  Backend Development  >  How to add text to pictures in php

How to add text to pictures in php

PHPz
PHPzOriginal
2023-04-26 10:31:122383browse

PHP is a popular open source server-side scripting language often used to develop dynamic websites and web applications. Among them, pictures are one of the essential elements in website design, and adding text to pictures can achieve better results. In this article, we will discuss how to add text on images, and based on this, we will look into adding text in different fonts in PHP.

Add text to pictures
Before adding text to pictures, we first need to ensure that we have mastered the GD library in PHP. The GD library is a library for programmatically generating dynamic images, usually used to generate pictures, buttons, animations, etc. If our PHP installation does not have the GD library, then we can install it first to facilitate subsequent operations.

Now, let us take a look at the basic steps of adding text to an image in PHP:

  1. Create a blank image and set the height, width, color, etc. of the image Properties;
$image = imagecreatetruecolor(400, 300);
$backgroundColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $backgroundColor);
  1. Set the size, color, position and other properties of the text;
$textColor = imagecolorallocate($image, 0, 0, 0);
$fontSize = 20;
$x = 50;
$y = 50;
$text = "Hello World!";
  1. Add text on the picture;
imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFilePath, $text);
  1. Return image resources and output.
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);

The specific implementation method of these steps is as follows:

<?php
  //创建一个空白的图片
  $image = imagecreatetruecolor(400, 300);
  //设置背景颜色
  $backgroundColor = imagecolorallocate($image, 255, 255, 255);
  //填充颜色
  imagefill($image, 0, 0, $backgroundColor);
  //设置文字的属性
  $textColor = imagecolorallocate($image, 0, 0, 0);
  $fontSize = 20;
  $x = 50;
  $y = 50;
  $text = "Hello World!";
  //向图片上添加文字
  imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFilePath, $text);
  //返回图片资源并输出
  header(&#39;Content-Type: image/png&#39;);
  imagepng($image);
  imagedestroy($image);
?>

In the above code, $fontFilePath represents the path of the font file, which is the key parameter to determine the font type. If we want to use different fonts in PHP, we need to provide the corresponding font file path.

Using different font types
Now that we have learned the basic method of adding text to images in PHP, we will look at how to use different font types.

In PHP, we can use TrueType font files to control the appearance of text. When adding text to pictures, we can set different font file paths according to specific needs. Specifically, we can use the imagettftext() function to add TrueType fonts to add custom font text to the image.

The following code is an example of adding text using different font files:

<?php
  //创建一个空白的图片
  $image = imagecreatetruecolor(400, 300);
  //设置背景颜色
  $backgroundColor = imagecolorallocate($image, 255, 255, 255);
  //填充颜色
  imagefill($image, 0, 0, $backgroundColor);
  //设置文字的属性
  $textColor = imagecolorallocate($image, 0, 0, 0);
  $fontSize = 20;
  $x = 50;
  $y = 50;
  $text = "Hello World!";
  //定义不同的字体文件
  $fontFilePath = "/path/to/font.ttf";
  //向图片上添加文字
  imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFilePath, $text);
  //返回图片资源并输出
  header(&#39;Content-Type: image/png&#39;);
  imagepng($image);
  imagedestroy($image);
?>

It should be noted that the font file we choose must be a font that supports the TrueType format before it can be used. In addition, the path to the font file needs to be set to $fontFilePath. If you are using different font files, you only need to enter the corresponding font file path.

Summary
In website design, adding pictures and text can make the website beautiful and orderly. In PHP, the GD library is a convenient way for us to add custom text. In this article, we have introduced ways to add text to images in PHP and discussed how to use different font types. The main steps we need to master include creating a blank image, setting text properties, adding text to the image and outputting the result. We hope this article helped you add images and text to your website design.

The above is the detailed content of How to add text to pictures in php. 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