The GD Library in PHP supports Chinese, but must be passed in UTF-8 encoding format, if you use the imagesString () function to draw Chinese characters directly will appear garbled, this is because GD2 pairs of Chinese can only receive UTF-8 encoding format, and the default use of English fonts, so to output a Chinese string, must be on the Chinese string transcoding, and set the Chinese string using the font, otherwise, the output can only contain garbled characters!
How to add text to images using the GD2 function (PHP graphic image typical application tutorial 2)
The GD Library in PHP supports Chinese, but must be passed in UTF-8 encoding format, if you use the imagesString () function to draw Chinese characters directly will appear garbled, this is because GD2 pairs of Chinese can only receive UTF-8 encoding format, and the default use of English fonts, so to output a Chinese string, must be on the Chinese string transcoding, and set the Chinese string using the font, otherwise, the output can only contain garbled characters!
In the previous article "creating images and colors and filling background colors (PHP graphic image typical application tutorial 1)", we introduced how to create images, colors, and fills, these are the first steps required to create images. today we will introduce how to add text to images!
When we add the desired text to the desired image, we need to use the imagettftext () function. Next let's take a look at the syntax format of this function:
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color, string $fontfile , string $text )
The following describes how to add text to an image using the imagettftext () function. The development procedure is as follows:
1. use the header () function to define the type of the output image.
2. use the imagecreatefromjpeg () function on the part.
3. use the imagecolorallocate () function to set the output font color.
4. define the font used for the output Chinese string.
5. use the imagettftext () function to add text to the image.
6. create an image and then reinforce the resource.
The code is as follows:
The output result is as follows:
Tip: you can use this method to create an electronic album!
Here we will introduce how to use the image to generate a verification code. For more information, see "Use image processing technology to generate a verification code (PHP graphic image typical application tutorial 3)".
The preceding figure shows how to use the GD2 function to add text to an image (PHP graphic image typical application tutorial 2). For more information, see other related articles in the first PHP community!