How to use the GD2 function to add text to a picture (PHP graphic image typical Application tutorial 2)

Source: Internet
Author: User
How to use the GD2 function to add text to a picture (PHP graphic image typical Application tutorial 2)

The GD Library in PHP supports Chinese, but it must be passed in UTF-8 encoding format, if the imagesstring () function is used to draw Chinese characters directly, it will be garbled, because GD2 can only receive UTF-8 encoding format, and English font is used by default. So to output Chinese strings, the Chinese string must be transcoded, and set the font used in Chinese strings, otherwise, the output can only be garbled!

In the previous article, "Creating images and colors as well as a detailed description of the fill background (PHP graphic image of the typical application tutorial 1)", we introduced the creation of images, colors and fills, these are the creation of any kind of image, must be the first step, then we today will show you how to add text on the picture!

When we add the desired text to the image we want, we'll use the Imagettftext () function, and we'll take a look at the syntax format of the function:

Array Imagettftext (Resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, Strin G $text)

Here we use the Imagettftext () function to add text to the image, with the following development steps:

1. Define the type of the output image through the header () function.

2. Pass the Imagecreatefromjpeg () function in the slice.

3. Set the output font color through the imagecolorallocate () function.

4. Define the font used for the output of the Chinese string.

5. Add text to the picture by using the Imagettftext () function.

6. Create the image and then fortification the resource.

The specific code is as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 "), header (' content-type:image/png ');//Tell the browser that this file is a PNG image $image = Imagecreatefromjpeg (" upfile/ Php.jpg ");//Create image//Fill Color-PS in the click Canvas fill $IMAGE_COLOLR =  imagecolorallocate ($image, 149, 188, 205); Imagefill ($image, 0, 0, $image _cololr); $black = Imagecolorallocate ($image,  105, 105, 105);//Text color Imagettftext ($image, 21, 0, 70, 220, $ Black, "Simhei.ttf", "PHP Chinese web www.php.cn");//Set Chinese text imagepng ($image);//Generate Picture Imagedestroy ($image);//Destroy picture, free memory?>

The results of the output are as follows:

Description under:

Tip: Use this method to create an electronic album!

The picture adds the text to introduce here, below we will introduce uses the image to generate the verification code, specifically reads "uses the image processing technology to generate the verification code (PHP graphic image's typical application tutorial 3)"!

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.