How to add text to images using the GD2 function (PHP graphic image typical application tutorial 2)

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.